7.1: The If Statement
The if statement is one way to make the sequence of computations executed by in an mfile script depend on variable values. The if statement has several different forms. The simplest form is

if expression

% Commands to execute if expression is true


end
where
expression
is a logical expression that is either true or false. (Information about logical expressions is available in the Logical Expressions section of this chapter.) For example, the following if statement will print "
v
is in fact negative:

if v < 0

disp('v is negative')


end
A more complicated form of the if statement is

if expression

% Commands to execute if expression is true


else

% Commands to execute if expression is false


end
For example, the following if statement will print "
v
is negative and "
v
is not negative:

if v < 0

disp('v is negative')


else

disp('v is not negative')


end
The most general form of the if statement is

if expression1

% Commands to execute if expression1 is true


elseif expression2

% Commands to execute if expression2 is true


elseif expression3

% Commands to execute if expression3 is true


...

else

% Commands to execute if all expressions are false


end
The following if statement is an example of this most general statement:

if v < 0

disp('v is negative')


elseif v > 0

disp('v is positive')


else

disp('v is zero')


end
Note that in all of the examples in this section, the commands inside the if statement are indented relative to the
if
,
else
,
elseif
,
and
end
statements. This is not required, but is common practice and makes the code much more readable.
Logical Expressions
Logical expressions are used in if statements, switchcase statements, and while loops to change the sequence of execution of commands in response to variable values. A logical expression is one that evaluates to either true or false. For example,
v > 0
is a logical expression that will be true if the variable
v
is greater than zero and false otherwise. (In mfile scripts, logical values (true and false) are actually represented by numerical values. The numerical value of zero represents false, and any nonzero numerical value represents true.)
Logical expressions are typically formed using the relational operators in Table 1.
Symbol  Relation 

< 
Less than 
<= 
Less than or equal to 
> 
Greater than 
>= 
Greater than or equal to 
== 
Equal to 
~= 
Not equal to 
Note that
==
is not the same as
=
; they are treated very differently in mfile scripting environments.
==
compares two values, while
=
assigns a value to a variable.
Complex logical expressions can be created by combining simpler logical expressions using the logical operators in Table 2.
Symbol  Relation 


Not 

And 

Or 
IfStatement Drill Exercises
Exercise 1
What will the following code print?


a = 10;

if a ~= 0

disp('a is not equal to 0')


end

Exercise 2
What will the following code print?


a = 10;

if a > 0

disp('a is positive')


else

disp('a is not positive')


end

Exercise 3
What will the following code print?


a = 5;

b = 3;

c = 2;

if a < b * c

disp('Hello world')


else

disp('Goodbye world')


end

Exercise 4
Suppose the code in Exercise 2 is modified by adding parentheses around
a > 0
to produce the following. What will it print?


a = 10;

if (a > 0)

disp('a is positive')


else

disp('a is not positive')


end

Exercise 5
Suppose the code in Exercise 3 is modfied by adding the parentheses shown below. What will it print?


a = 5;

b = 3;

c = 2;

if (a < b) * c

disp('Hello world')


else

disp('Goodbye world')


end

Exercise 6

What will the following code print?

p1 = 3.14;

p2 = 3.14159;

if p1 == p2

disp('p1 and p2 are equal')


else

disp('p1 and p2 are not equal')


end

Exercise 7
What will the following code print?


a = 5;

b = 10;

if a = b

disp('a and b are equal')


else

disp('a and b are not equal')


end

Exercise 8
For what values of the variable
a
will the following code print 'Hello world'?


if ~ a == 0

disp('Hello world')


else

disp('Goodbye world')


end

Exercise 9
For what values of the variable
a
will the following code print 'Hello world'?


if a >= 0 && a < 7

disp('Hello world')


else

disp('Goodbye world')


end

Exercise 10
For what values of the variable
a
will the following code print 'Hello world'?


if a < 3  a > 10

disp('Hello world')


else

disp('Goodbye world')


end

Exercise 11
For what values of the variable
a
will the following code print 'Hello world'?


if a < 7  a >= 3

disp('Hello world')


else

disp('Goodbye world')


end

Exercise 12
Write an if statement that will print 'a is very close to zero' if the value of the variable
a
is between
Notes/Highlights Having trouble? Report an issue.
Color  Highlighted Text  Notes  

Please Sign In to create your own Highlights / Notes  
Show More 