An interactive introduction to MATLAB

(Jeff_L) #1
4.2the if-else statement 53


  1. †What will the following code print?
    1 a = 10;
    2 if a ~= 0
    3 disp('a is not equal to zero')
    4 end

  2. †What will the following code print?
    1 a = 10;
    2 if a > 0
    3 disp('a is positive')
    4 else
    5 disp('a is not positive')
    6 end

  3. †What will the following code print?
    1 a = 5;
    2 b = 3;
    3 c = 2;
    4 if a < b*c
    5 disp('Hello world')
    6 else
    7 disp('Goodbye world')
    8 end

  4. †For what values of the variable will the following code printHello world?
    1 if a >= 0 & a < 7
    2 disp('Hello world')
    3 else
    4 disp('Goodbye world')
    5 end

  5. †For what values of the variable will the following code printHello world?
    1 if a < 7 | a >= 3
    2 disp('Hello world')
    3 else
    4 disp('Goodbye world')
    5 end


†Questions from Morrell, D., Programming with M-files: If-Statement Drill Exercises , Connex-
ions, http://cnx.org/content/m13432/1.4/, [Last assessed: Nov 2011]

Free download pdf