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

Choices and loops on the board

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

If this, then that

Computing 6: Unit 8, pp. 106–123
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.

setscoreto7show iconyesshow iconnoifscore>5thenelseon buttonApressed
Worked examples
scorescore > 5?PathBoard shows
3falseelseno
5falseelseno
9truethenyes
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.
setlevelto5show iconheartshow iconyesshow iconnoiflevel≥3thenelseiflevel≥8thenelseon buttonApressed
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.
SymbolMeaningExample with n = 7True or false
=equal ton = 7true
≠not equal ton ≠ 7false
<less thann < 9true
>greater thann > 7false
≤less than or equal ton ≤ 7true
≥greater than or equal ton ≥ 8false
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?
setlivesto4show iconghostshow iconheartiflives<1thenelseon buttonApressed
2
Trace the nested program for temp = 20. Then say what it shows for temp = 30 and for temp = 15.
settempto20show icont-shirtshow iconyesshow iconsadiftemp≥15thenelseiftemp≥30thenelseon buttonApressed
3
Write the blocks for this rule: if the number of tickets is more than 50, show 1, else show 0.
4
n is 7. Which of these conditions are true? (a) n < 7, (b) n ≤ 7, (c) n ≠ 7, (d) n > 6.