If Statement
From ComputingForScientists
Contents 
1. If statement
1.1. Objectives
 To introduce the use of the
if
statement.
1.2. Motivation
 A fundamental part of many algorithms is: "If something is true, do this. Else, do that." For example, a statement about population change could be, "If population is less than 10 billion, population next year is equal to 1.1 times the population this year. Else, the population next year is 0.1 times the population this year."
1.3. If statement syntax
The syntax for an if
statement is
if (test) Execute these commands else Execute these commands end
The above means that if the statement of test
is correct (true) then execute the commands between the if
and else
. If not, execute the commands between else
and end
.
The following is an example of a test
that is incorrect (false) because 37 is not less than 2:
if (37 < 2) Commands here get ignored else Commands here get executed end
In this case, the commands between the else
and end
are executed. The commands could be anything, like a for
loop or another if
statement.
When an if
statement is used, an else
is not required in this case, if the test
is false (and there is no else
statement) then the statement(s) between the if
and end
still do not execute. The code elseif
keyword can be used to test multiple statements:
if (test1) Execute these commands if test1 is true elseif (test2) Execute these commands if test2 is true elseif (test3) Execute these commands if test3 is true else Executed these commands if test1, test2, and test3 are false end
If more than one test is true, the commands associated with the earlier statement tested will be executed.
1.4. Test syntax
In addition to using numbers for x = 37 y = 2 then 

1.5. Examples
This program
x = 1; if (x < 3) x = 0; else x = 10; end x
is interpreted as "first assign to x
the value of 1
. Then assign it the value of 0
. The last line tells MATLAB to display the last value assigned to x
, which was 0
. Provided that the first value assigned to x
is less than 3, the above program is the same as:
x = 1; x = 0; x
What do you expect to happen if you execute this?
A(1) = 1; A(2) = 99; if (A(1) < 3) A(1) = 0; else A(1) = 10; end A
Answer 

A(1) = 1;
A(2) = 99;
if (A(1) < 3) %A(1) = 1 => Is 1 < 3? => True
A(1) = 0; %A(1) is reassigned a new value => A(1) = 0
else
A(1) = 10; %This statement is not executed because the
The last line of this code displays the following: A = 0 99 A short description of what happens when the program is executed: The 1st element of the array 
What do you expect to happen if you execute this?
x = 1; A(1) = 1; if (A(1) < 3) x = 0; else x = 10; end A x
Answer 

x = 1; A(1) = 1; if (A(1) < 3) %A(1) = 1 => Is 1 < 3? => True x = 0; % The last two lines of this program cause the following to be displayed: A = [1] x = 0 A short description of what happens when the program is executed:

What do you expect to happen if you execute this?
A = [1,0,1,0]; if (mean(A) > 1) a = 1 else a = 2 end
Answer 


1.6. If statement and matrices
The test
in an statement
is typically a statement that is either a logical zero or one and the results of a test can be stored in a variable. For example
b = 2 test = b > 1 if (test) b = 3 end
gives
test = 1 b = 3
Note that the class of test
is logical while the class of b
is double. (To see this enter whos test
and whos b
.
MATLAB allows test
to be a matrix. In this case, if all of the elements of test
are nonzero, test
is considered true.
A = [1,2,3]; if (A) a = 1; % This statement is executed if all elements of A are nonzero. else a = 2; end
Note that A
may not contain complex numbers.
1.7. Conditional statements and matrices
A conditional statement that includes a matrix results in a matrix with elements that satisfy the conditional statement having a value of logical 1 and other elements having a value of logical 0. For example,
M = eye(3) % Create a matrix with 0s and 1s M = 1 0 0 0 1 0 0 0 1 M2 = M == 0 % M == 0 is a matrix with logical 1s where the statement is true. Give this matrix the name M2.
gives
M2 = 0 1 1 1 0 1 1 1 0
because the equality test is performed on each element of the matrix. When the equality is true, the corresponding element in the new matrix has a value of 1. When the equality is false, the corresponding element in the new matrix has a value of 0.
2. Problems
2.1. Syntax
a = 1; if (a == 1) b = 2; if (b == 2) c = 3; end end c
Answer 

a = 1; if (a == 1) %a = 1 => Is 1 == 1? True b = 2; %b is assigned the value 2 if (b == 2) %b = 2 => Is b == 2? True c = 3; %c is assigned the value 3 end end c The last line of the program displays the following: c = 3 Note that two equal signs means "equal to" while one equal sign means assignment. 
clear; a = 1; if (a == 2) if (a == 1) b = 2; end end b
Answer 

a = 1; if (a == 2) %a = 1 => Is 1 == 2? => False if (a == 1) %since the first The last line will result in an error because the variable 
M(10,10) = 0; counter = 37; if (M(10,10) == 1) counter = 1; end counter
Answer 

M(10,10) = 0;
counter = 37;
if (M(10,10) == 1) %M(10,10) = 0 => Is 0 == 1? => False
counter = 1; %This statement is not executed because the
The last line of the program displays the following: counter = 37 
A = [1,0,3,0,5,0,7,0,9,0,11,0,13]; for i = [1:13] if(A(i) == 0) A(i) = A(i1); end end A
Answer 

If A(i) is assigned the value 0 then that position, A(i), is reassigned the value in the previous position, A(i1): A = [1,0,3,0,5,0,7,0,9,0,11,0,13]; i = 1 if(A(i) == 0) %A(1) = 1 => Is 1 == 0? => False A(i) = A(i1); %This statement does not execute because the When executed, this program will result in the following array: A = [1 1 3 3 5 5 7 7 9 9 11 11 13] 
A = [0,0,0,0,0,0,0,0,0,0,0,0]; for i = [2:2:12] if(A(i) == 0) A(i) = i2; end end A
Answer 

A = [0,0,0,0,0,0,0,0,0,0,0,0]; i = 2 if(A(i) == 0) %A(2) = 0 => Is 0 == 0? => True A(i) = i2; %A(2) = 22 => A(2) = 0 end i = 4 if(A(i) == 0) %A(4) = 0 => Is 0 == 0? => True A(i) = i2; %A(4) = 42 => A(4) = 2 end i = 6 if(A(i) == 0) %A(6) = 0 => Is 0 == 0? => True A(i) = i2; %A(6) = 62 => A(6) = 4 end ... i = 12 if(A(i) == 0) %A(12) = 0 => Is 0 == 0? => True A(i) = i2; %A(12) = 122 => A(12) = 10 end When the program is executed, this is displayed: A = [0 0 0 2 0 4 0 6 0 8 0 10] 
A = [1,0,1,0,1,0,1,0,0,1,1,1]; for i = [1:12] if(A(i) == 0) A(i) = A(i1); end end
Answer 

There are semicolons after the statements so there is no output but this is what is happening: i = 1 if(A(i) == 0) %A(1) = 1 => Is 1 == 0? => False A(i) = A(i1); %This statement is not executed because the If array A = [1 1 1 1 1 1 1 1 1 1 1 1] 
for i = [2:2:12] if(A(i) == 1) A(i) = 0; end end A
Answer 

The array A was not defined before it was used in the 
A = [1,0,1,0,1,0,1,0,0,1,1,1]; for i = [2:12] if(A(i) == 0) if(A(i1) == 1) A(i) = A(i1); end end end for i = [12:1:2] if(A(i) == 1) if(A(i1) == 0) A(i1) == 0.5; end end end A
Answer 

There are 2 parts to this program. Each part of the program has one A = [1,0,1,0,1,0,1,0,0,1,1,1]; %%%First The last line of the program will display the following: A = [1 1 1 1 1 1 1 1 1 1 1 1] 
These questions are intended to be worked in order, as each question builds on the previous question. To answer these questions, please refer to If_Statement#Slides, your notes from class, and If_Statement#Tutorial.
2.2. Syntax errors
Identify the error that prevents the following programs from executing.
a = 1; if (a = 1) b = 2; end a
Answer 


a = 1; if (a =< 1) b = 2; end a
Answer 

Syntax is 
2.3. Assignment versus relational expressions
I start MATLAB and enter three commands a = 1
, a == 3+5
, and a = 3+5
.
>> a = 1 >> a == 3+5 ans = 0 >> a = 3+5 a = 8
Explain why the results are different.
Answer 

The second command assigns the value of 8 to a variable named Note that if we wanted to assign to a variable named 
2.4. elseif
Predict the output when the following commands are executed
a = 1; if (a > 0) b = 1 elseif (a == 1) b = 2 else b = 3 end
Answer 


2.5. If statement and matrices
Predict the output of the commands
M = ones(3) % Create a 3x3 matrix with all 1s. if (M) a = 1 else a = 2 end
Answer 

All elements of 
2.6. If statement and matrices
Predict the output of the commands
M = ones(3) % Create a 3x3 matrix with all 1s. if (M) a = 1 M(1,1) = 0; if (M) a = 2 end else a = 3 end
2.7. If statement and matrices
Are the following two statements equivalent for any matrix M
that contains real values?
if (M ~= 0) a = 1; end
if (M) a = 1; end
2.8. Conditional statements and matrices
Predict the output of the commands
M = eye(3) (M == 0) + M
2.9. Conditional statements and matrices
Given a matrix M
, are any of the following statements equivalent?
M(find(M== 0)) = 1; M(find(M) == 0) = 1; M(M == 0) = 1;
Answer 

The first and last are equivalent.
To test this, use M = randi(10,10)  5; % Create matrix of random integers in range 4 through 5. M1 = M; % Create copy of original matrix. M2 = M; M3 = M; M1(find(M == 0)) = 1; M2(find(M) == 0) = 1; M3(M == 0) = 1; M1M2 % Does not always have all zeros M1M3 % Always has all zeros 
2.10. Modifying an array
Write a function that takes an input of an array with arbitrary values, a value to replace, and the replacement value and returns the modified array. For example, executing
y = [9,10,99,4,3]; r = 99; n = 0; z = replace(y,r,n)
will display
z = 9 10 0 4 3
Your function should work for any array y
and any scalar values for r
and n
. Save your function in a file named replace.m
. Also script named replace_test
that contains tests that you used to check your function.
3. Activities
3.1. Interpreting an if statement
On this sheet of paper, write out the matrix that will be created when this program is executed:
counter = 0; M(40,40) = 0; for i = [1:40] for j = [1:40] if (M(i,j) > 150) M(i,j) = 100; counter = counter+1; end end end
Open a new file named ifprobI.m
and enter the above to check your answer.
Open a file named ifprobII.m
and rewrite a version of the above that detects those positions in matrix M
that have values exactly equal to 0. If such a value is detected, reassign that position's value to 100. Report the value of counter
that's printed to the screen.
Answer 

counter = 0; M(40,40) = 0; for i = [1:40] for j = [1:40] if (M(i,j) == 0) M(i,j) = 100; counter = counter+1; end end end counter 
3.2. Predicting Output
Read through the following programs and try to predict the values of M
and count
that will be displayed. Try running these programs after guessing what will happen when you execute them. Then make changes to the programs and try to predict the result.
3.2.1.
clear; clc; count = 0; M(5,5) = 0; for i=[1:5] for j=[1:5] if (M(i,j) == 0) M(i,j) = 1; count = count+ 1; end end end M count 
clear; clc; count = 0; M(5,5) = 0; for i=[1:5] for j=[1:5] if (i == 2) M(i,j) = 1; count = count+ 1; end end end M count 
clear; clc; count = 0; M(5,5) = 0; for i=[1:5] for j=[1:5] if (j > i) M(i,j) = 1; count = count+ 1; end end end M count 
3.2.2.
Write a program with a nested for
loop and one or more if
statements that will produce the following output. If you have problems, write out your program in longhand to help you figure out why you are not getting the expected answer.
1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
Answer 

clear; clc; M(5,5) = 0; for i=[1:5] for j=[1:5] if (i == j) M(i,j) = 1; end end end M 
3.2.3.
Write a program with a nested for
loop and one or more if
statements that will produce the following output. If you have problems, write out your program in longhand to help you figure out why you are not getting the expected answer.
1 0 0 0 0 1 1 0 0 0 1 1 1 0 0 1 1 1 1 0 1 1 1 1 1
Answer 

clear; clc; M(5,5) = 0; count = 0; for i=[1:5] for j=[1:5] if (i >= j) M(i,j) = 1; count = count + 1; end end end M count 
3.2.4.
Write a program with a nested for
loop and one or more if
statements that will produce the following output. If you have problems, write out your program in longhand to help you figure out why you are not getting the expected answer.
0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Answer 

clear; clc; M(5,5) = 0; count = 0; for i=[1:5] for j=[1:5] if (i == 2) M(i,j) = 1; count = count + 1; end end end M count 