GoalDescribe an if … then … else statement and say which path is carried out when the condition is true or false.
New words
else · the part of a statement that is carried out when the condition is falseif … then … else · a statement that chooses between two pathspath · one of the ways that a program can goexactly one · one and only one, not none and not both
Explanation
An if … then … else statement chooses between two paths. It has three parts. The condition comes after ‘if’. Decision 1 comes after ‘then’ and is carried out only when the condition is true. Decision 2 comes after ‘else’ and is carried out only when the condition is false. So exactly one of the two decisions happens every time. For example: ‘If the library is open, then we borrow a book, else we play outside.’ The program checks the condition first and then follows one path.
Worked examples
Statement
Condition
If true
If false
If the lift is working, then use the lift, else use the stairs.
the lift is working
use the lift
use the stairs
If the bag weighs less than 5 kg, then carry it, else use a trolley.
the bag weighs less than 5 kg
carry it
use a trolley
If the bread is sold out, then buy rolls, else buy bread.
the bread is sold out
buy rolls
buy bread
Each statement has a condition, a ‘then’ outcome and an ‘else’ outcome.
Scratch has an if … then … else block. This script moves a Kite up by 5 steps when the up arrow key is pressed, and down by 2 when it is not. The key is checked each second. If the key is held at checks 1, 2 and 4 and not held at checks 3 and 5, the Kite goes +5, +5, −2, +5, −2 and ends at y: 11.
Block
Condition true
Condition false
if … then
carries out the blocks inside
does nothing
if … then … else
carries out the ‘then’ blocks
carries out the ‘else’ blocks
An if … then block does something only when the condition is true. An if … then … else block always does something.
Class activity
Branching walk (groups). Mark a start line on the floor and two doors, a red ‘then’ door and a blue ‘else’ door. A leader reads out cards such as ‘If the number on your card is even, then walk through the red door, else walk through the blue door’. Players hold number cards and walk. Invent cards about colours or animals too. Check: does anyone ever go through both doors, or neither? Why not?
Practice
1
Find the condition, the ‘then’ outcome and the ‘else’ outcome. (a) If the stall has mangoes, then we buy a kilo, else we buy bananas. (b) If the shoes fit, then she wears them, else she gives them away.
(a) Condition: the stall has mangoes. Then: we buy a kilo. Else: we buy bananas. (b) Condition: the shoes fit. Then: she wears them. Else: she gives them away.
2
The Fish starts at y: 0. At each check the down arrow key is held at checks 1, 2 and 4, and not held at checks 3 and 5. Where is the Fish after 5 checks?
-10, -10, +3, -10, +3 gives -24. The Fish is at y: -24.
3
What is said when the number typed is 9? When it is 10? When it is 11?
9: Small. 10: Small, because 10 is not more than 10. 11: Big.
4
Why does an if … then … else block always carry out exactly one of its two parts? Give an example where plain if … then is not enough.
The condition is either true or false, so one path runs. Plain if … then is not enough when something must happen in both cases, such as ‘if it rains then take an umbrella, else wear sunglasses’.