# 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 test, one can compare variables that hold a number. For example, if

x = 37
y = 2


then test could be anything in the second column of this table.

test syntax Meaning
x < y is x less than y?
x > y is x greater than y?
x == y is x equal to y?
x <= y is x less than or equal to y?
x >= y is x greater than or equal to y?
x ~= y is x not equal to y?

## 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


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


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


## 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 non-zero, test is considered true.

A = [1,2,3];
if (A)
a = 1; % This statement is executed if all elements of A are non-zero.
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

clear;
a = 1;
if (a == 2)
if (a == 1)
b = 2;
end
end
b

M(10,10) = 0;
counter = 37;
if (M(10,10) == 1)
counter = 1;
end
counter

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(i-1);
end
end
A

A = [0,0,0,0,0,0,0,0,0,0,0,0];
for i = [2:2:12]
if(A(i) == 0)
A(i) = i-2;
end
end
A

A = [1,0,1,0,1,0,1,0,0,1,1,1];
for i = [1:12]
if(A(i) == 0)
A(i) = A(i-1);
end
end

for i = [2:2:12]
if(A(i) == 1)
A(i) = 0;
end
end
A

A = [1,0,1,0,1,0,1,0,0,1,1,1];
for i = [2:12]
if(A(i) == 0)
if(A(i-1) == 1)
A(i) = A(i-1);
end
end
end
for i = [12:-1:2]
if(A(i) == 1)
if(A(i-1) == 0)
A(i-1) == 0.5;
end
end
end
A


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


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


## 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.

## 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


## 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


## 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;


## 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.

## 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 long-hand 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


### 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 long-hand 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


### 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 long-hand 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