GoalUse if–then and if–then–else blocks so that a program chooses between paths, and trace which path runs.
New words
selection · a choice in a program, so that different blocks run in different casescondition · a question that is true or false, such as score > 5comparison operator · a symbol that compares two values: =, ≠, <, >, ≤ or ≥path · one of the ways that a program can go after a choice
Explanation
Selection lets a program choose. An if–then block has a condition. When the condition is true the blocks inside run. When it is false they are skipped. An if–then–else block has two paths, and exactly one of them runs. The condition compares two values with an operator. These are = (equal), ≠ (not equal), < (less than), > (greater than), ≤ (less than or equal) and ≥ (greater than or equal). To trace a program, work out the condition first, then follow only the path that it chooses. Always check both paths. If we need more than two outcomes, we can put another if block inside the else path.
Worked examples
score
score > 5?
Path
Board shows
3
false
else
no
5
false
else
no
9
true
then
yes
The condition in the explanation is score > 5. Trace it with three different scores. Notice that 5 is not greater than 5, so the else path runs.
This program has three outcomes. If the water is at 8 or more it shows a heart. If not, it asks a second question, and shows yes for 3 or more and no for less. With a level of 5, the first condition is false and the second is true, so it shows yes.
Symbol
Meaning
Example with n = 7
True or false
=
equal to
n = 7
true
≠
not equal to
n ≠ 7
false
<
less than
n < 9
true
>
greater than
n > 7
false
≤
less than or equal to
n ≤ 7
true
≥
greater than or equal to
n ≥ 8
false
These are the six comparison operators, each tried with n = 7. Some of the examples are true and some are false.
Class activity
If–else game (no computer): write ten numbers from 0 to 20 on cards. One player is the board. Make a rule card, such as ‘if the number is greater than 12, clap, else stamp’. Draw the cards one at a time and the board does the action. Write the results in a table. Which cards were close to 12? Then make a rule with three outcomes using an else inside an else, for example for numbers over 15, over 8 and the rest. If you have a computer or tablet, build the program in a micro:bit simulator.
Practice
1
What does the board show for the program as it is? What would it show if the number in the set block were changed to 0?
With lives = 4, the condition lives < 1 is false, so the else path runs and the board shows a heart. With 0, the condition is true, so the then path runs and the board shows a ghost.
2
Trace the nested program for temp = 20. Then say what it shows for temp = 30 and for temp = 15.
temp = 20: the first condition (temp ≥ 30) is false, so the else path runs. The second condition (temp ≥ 15) is true, so it shows yes. temp = 30: the first condition is true, so it shows the t-shirt and skips the else path. temp = 15: the first condition is false and the second is true (15 ≥ 15), so it shows yes.
3
Write the blocks for this rule: if the number of tickets is more than 50, show 1, else show 0.
if <(tickets) > (50)> then show number (1) else show number (0).
4
n is 7. Which of these conditions are true? (a) n < 7, (b) n ≤ 7, (c) n ≠ 7, (d) n > 6.