AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

If, then, else

Week 19 · Unit 8: Be an innovator · 3 lessons
1

Two paths: if, then, else

Computing 5: Unit 8, pp. 104–118
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.

Startis the library open?yesnoborrow a bookplay outsideEnd
Worked examples
StatementConditionIf trueIf false
If the lift is working, then use the lift, else use the stairs.the lift is workinguse the liftuse the stairs
If the bag weighs less than 5 kg, then carry it, else use a trolley.the bag weighs less than 5 kgcarry ituse a trolley
If the bread is sold out, then buy rolls, else buy bread.the bread is sold outbuy rollsbuy bread
Each statement has a condition, a ‘then’ outcome and an ‘else’ outcome.
Kitewhenclickedgo to x:0y:0wait1secondschange y by5change y by-2ifkeyup arrowpressed?thenelseforever
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.
BlockCondition trueCondition false
if … thencarries out the blocks insidedoes nothing
if … then … elsecarries out the ‘then’ blockscarries 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.
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?
Fishwhenclickedwait1secondschange y by-10change y by3ifkeydown arrowpressed?thenelseforever
3
What is said when the number typed is 9? When it is 10? When it is 11?
Starttype a numbernumber > 10?yesnosay Bigsay SmallEnd
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.