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:
- Write conditional statements in MATLAB and combine these using logical connectors for AND, OR and NOT;
- Use if statements to perform commands based on logical conditions.
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) (ii) (iii) (iv) (v) (vi) 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) (ii) (iii) 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). 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.
fprintf("%1i is less than %1i.", a, b)
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.
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.
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.
fprintf("Good morning, how are you today?")
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.
fprintf("%1i is divisible by 3.", x)
fprintf("%1i is not divisible by 3.", x)
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
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.
fprintf("The value for the hour should be between 0 and 23.")
fprintf("Good morning, how are you today?")
fprintf("Good afternoon, are you having a good day?")
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.
fprintf("%1i is a positive number.", x)
fprintf("%1i is a negative number.", x)
fprintf("%1i is zero.", x)
4. Write a program that prints the degree classification based on a specified average mark using the following mark bands.
if avg_mark < 0 || avg_mark > 100
fprintf("The average mark should be a percentage, check your calculations.")
fprintf("Upper second class (2:1)")
fprintf("Lower second class (2:2)")
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 discriminant = b ^ 2 - 4 * a * c;
fprintf("There is one root at x=%1.4f.", root1)
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.
fprintf("%02d:%02d is %1i:%02dam.", hours, minutes, hours, minutes)
fprintf("%02d:%02d is %1i:%02dpm.", hours, minutes, hours - 12, minutes)