10.3. 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
if condition
% commands executed when true
end
Note that the unlike Python, MATLAB uses the end
keyword to denote the end of the if statement. Enter the following code into your program.
% If statements
x = 2;
y = 3;
if x < y
fprintf("%d is less than %d\n", x, y)
end
Run your program and your should see the following added to the console output.
2 is less than 3
10.3.1. Else-if#
Sometimes you want to perform calculations if the previous conditions were not true. We can do this using the if-else condition with the elif
keyword.
if condition1
% commands executed if condition1 is true
elseif condition2
% commands executed if condition2 is true
end
Enter the following code into your program.
% Else-if statements
hour = 15;
if hour < 12
fprintf("Good morning, how are you today? \n")
elseif hour < 18
fprintf("Good afternoon, are you having a good day? \n")
end
Run your program and your should see the following added to the console output.
Good afternoon, are you having a good day?
Try changing the value of hour
to see the different results.
10.3.2. Else#
The else
keyword is used to perform calculations if all previous conditions are false.
if condition1:
% commands executed if condition1 is true
elseif condition2:
% commands executed if condition 2 is true
else
% commands executed if all previous conditions are false
end
Add the following code to your if-else statement
else
fprintf("Good evening, did you have a good day? \n")
and change the value of the hour
variable to 21. Run your program and your should see the following added to the console output.
Good evening, did you have a good day?
10.3.3. Exercises#
Create a new MATLAB file called M10_If_statements_exercise.m and save it to your OneDrive folder. Use it to answer the following exercises.
The degree classification for an undergraduate student at Manchester Metropolitan University is determined using by calculating a weighted average of the level 5 (2nd year) and level 6 (3rd year) unit marks using the ratio 25:75 then looking up the classification in the table below.
Weighted average |
Degree classification |
---|---|
\(\geq 70\) |
first class (1st) |
\(\geq 60\) |
upper second class (2:1) |
\(\geq 50\) |
lower second class (2:2) |
\(\geq 40\) |
third class (3rd) |
\(<40\) |
fail |
For example, a student who has gained level 5 marks of 55, 45, 75, 65 and level 6 marks of 60, 74, 72, 68 has a weighted average of 66.4 and therefore will graduate with upper second class honours.
Write a program that prints the degree classification based on the level 5 and 6 marks that are stored in two arrays. Use your program to determine the degree classification of a student who has level 5 marks of 62, 65, 72, 64 and level 6 marks of 66, 73, 77, 70.
The degree classification of a student is also determined using the profile method that only uses the level 6 marks. The average of the level 6 marks and the degree classification is found by looking up the table below.
Level 6 average |
together with two units marks of |
Degree classification |
---|---|---|
\(\geq 68\) |
\(\geq 70\) |
first class (1st) |
\(\geq 58\) |
\(\geq 60\) |
upper second class (2:1) |
\(\geq 48\) |
\(\geq 50\) |
lower second class (2:2) |
\(\geq 40\) |
\(\geq 40\) |
third class (3rd) |
The degree classification a student receives is the higher classification of the weighted average method and the profile method.
For example, for the same student with level 6 marks of 60, 74, 72, 68 giving a level 6 average of 68.5. Since they have 2 units with marks above 70% the classification using the profile method is first class honours.
Extend your program from Exercise 10.1 so that the degree classification using the profile method is determined and the final degree classification is printed.
Hint: The sort(x)
function sorts the array x
into ascending order.
Write a program that plays the rock-paper-scissors game. Your program should select from rock, paper or scissors at random and based on a choice you have made determine who has won and prints an appropriate message to the console, e.g.,
Exercise 3.3
------------
You have chosen rock
Your opponent has chosen scissors
Rock crushes scissors, you win!
Hint: the randi(n)
function generates a random integer between 1 and n
.