Linear Algebra and Programming Skills

Dr Jon Shiach, Department of Computing and Mathematics, Manchester Metropolitan University

If statements

Learning Outcomes

On successful completion of this page readers will be able to:
A common task in programming is to perform an operation if a particular condition is met. If statements allow us to do this and are an integral part of computer programming.

Conditional statements

Conditional statements are statements which we can use to make decisions based on its result. They return a Boolean value of either 1 (true) or 0 (false) depending on what condition we have applied.
Example 1
Use MATLAB commands to determine the output of the following logical statements:
(i)
2 == 3
2 == 3
ans = logical
0
(ii)
2 ~= 3
2 ~= 3
ans = logical
1
(iii)
2 > 3
2 > 3
ans = logical
0
(iv)
2 < 3
2 < 3
ans = logical
1
(v)
2 >= 2
2 >= 2
ans = logical
1
(vi)
3 <= 2.9999
3 <= 2.9999
ans = logical
0

Logical Connectors

The MATLAB commands for standard logical connectors AND, OR and NOT are given below (help page).
Example 2
Use MATLAB commands to determine the result of the following logical statements
(i)
2 < 3 || 3 == 4
2 < 3 || 3 == 4
ans = logical
1
(ii)
3 > 2 && 2 ^ 2 == 4
3 > 2 && 2 ^ 2 == 4
ans = logical
1
(iii)
~ (9 / 3 == 3)
~ 9 / 3 == 3
ans = logical
0

If statements

If statements are used in a program to execute commands when a certain logical condition is met. The syntax of an if statement takes the following form (help page).
if conditional statement
commands executed when true
end
Note that it is good programming practice to indent any commands which are contained within an if statement.
Example 3
The commands below defines two variables a and b and prints a sentence if a < b. Enter them into the code cell below and run the live script to see the result. Try changing the values of a and b to see what effect it has.
a = 2;
b = 3;
 
if a < b
fprintf("%1i is less than %1i.", a, b)
end
clear
a = 2;
b = 3;
 
if a < b
fprintf("%1i is less than %1i.", a, b)
end
2 is less than 3.

Exercise 1 - If statements

1. Use an if statement to halve the integer x if it is an even number.
Hint: the mod command will come in useful here.
clear
x = 4;
 
if mod(x, 2) == 0
x / 2
end
ans = 2

If-else statements

If you want to execute some commands if the logical condition is met but others if it is not, we can use an if-else statement.
if conditional statement
commands executed when true
else
commands executed when false
end
Example 4
The commands below defines the variable hour and prints a sentence based on whether its value is greater or less than 12. Enter them into the code cell below and run the live script to see the result. Try changing the value of hour to see what the effect is.
hour = 10;
 
if hour < 12
fprintf("Good morning, how are you today?")
else
fprintf("Good afternoon, how are you today?")
end
hour = 10;
 
if hour < 12
fprintf("Good morning, how are you today?")
else
fprintf("Good afternoon, how are you today?")
end
Good morning, how are you today?

Exercise 2 - If-else statements

2. Use an if-else statement to write a program to print a statement to say whether or not a number is divisible by 3.
clear
x = 6;
 
if mod(x, 3) == 0
fprintf("%1i is divisible by 3.", x)
else
fprintf("%1i is not divisible by 3.", x)
end
6 is divisible by 3.

If-elseif-else statements

We can specify further conditions in the event that the first condition is not met using an if-elseif-else statement
if conditional statement
commands executed when true
elseif another conditional statement
commands executed when true
else
command executed when none of the conditional statements are true
end
Example 5
The following commands defines the variable hour and uses if-elseif-else statements to check whether its value is valid (for a 24-hour clock) and prints a sentence based on this value. Enter them into the code cell below and run the live script to see the result. Try changing the value of hour to see what the effect is.
hour = 21;
 
if hour < 0 || hour > 23
fprintf("The value for the hour should be between 0 and 23.")
elseif hour < 12
fprintf("Good morning, how are you today?")
elseif hour < 18
fprintf("Good afternoon, are you having a good day?")
else
fprintf("Good evening, did you have a good day?")
end
hour = 21;
 
if hour < 0 || hour > 23
fprintf("The value for the hour should be between 0 and 23.")
elseif hour < 12
fprintf("Good morning, how are you today?")
elseif hour < 18
fprintf("Good afternoon, are you having a good day?")
else
fprintf("Good evening, did you have a good day?")
end
Good evening, did you have a good day?

Exercise 3 - If-elseif-else statements

3. Write a program that determines whether a number is positive, negative or zero and prints the result.
clear
x = 2;
 
if x > 0
fprintf("%1i is a positive number.", x)
elseif x < 0
fprintf("%1i is a negative number.", x)
else
fprintf("%1i is zero.", x)
end
2 is a positive number.
4. Write a program that prints the degree classification based on a specified average mark using the following mark bands.
clear
avg_mark = 65;
 
if avg_mark < 0 || avg_mark > 100
fprintf("The average mark should be a percentage, check your calculations.")
elseif avg_mark >= 70
fprintf("1st class")
elseif avg_mark >= 60
fprintf("Upper second class (2:1)")
elseif avg_mark >= 50
fprintf("Lower second class (2:2)")
elseif avg_mark >= 40
fprintf("Third class")
else
print("Fail")
end
Upper second class (2:1)
5. Write a program that calculates the real roots of a quadratic polynomial given the values of a, b and c for using the quadratic formula
The program should determine whether there are two, one or no real roots. Test your program with the following quadratic equations
(a) which has roots at and
(b) which has a single root at
(c) which has no real roots
clear
a = 1;
b = -4;
c = 2;
 
discriminant = b ^ 2 - 4 * a * c;
if discriminant < 0
fprintf("No real roots")
elseif discriminant == 0
root1 = -b / (2 * a);
fprintf("There is one root at x=%1.4f.", root1)
else
root1 = (-b - sqrt(discriminant)) / (2 * a);
root2 = (-b + sqrt(discriminant)) / (2 * a);
fprintf("There are two roots at x=%1.4f and x=%1.4f.", root1, root2)
end
There are two roots at x=0.5858 and x=3.4142.
6. Write a program that converts a time given in hours and minutes using the 24-hour format to 12 hour format. e.g., 15:05 would become 3:05pm.
Hint: use fprintf('%02d', x) to output a leading zero if x is less than 10.
clear
hours = 15;
minutes = 5;
 
if hours < 12
fprintf("%02d:%02d is %1i:%02dam.", hours, minutes, hours, minutes)
else
fprintf("%02d:%02d is %1i:%02dpm.", hours, minutes, hours - 12, minutes)
end
15:05 is 3:05pm.