Week 10 · Unit 4: Variables in programming · 3 lessons
2
Counting and loops
Computing 6: Unit 4, pp. 52–67
GoalUse a variable as a counter in a loop and trace a countdown.
New words
counter · a variable that counts how many times something happenscountdown · counting down to 0, or to a chosen end numberrepeat until · a loop that keeps repeating until its condition becomes trueinfinite loop · a loop that never stops
Explanation
A variable can count. A counter starts at a value, such as 0, and a step inside a loop changes it each time round. With change by 1 the counter counts up. With change by −1 it counts down. The block repeat until checks its condition before each turn of the loop. When the condition becomes true the loop ends. The loop must contain a block that changes the variable that the condition tests. If nothing changes it, the condition never becomes true and the loop is an infinite loop. A forever loop is useful for a job that never has to stop, such as giving a player coins every few seconds.
Worked examples
Turn
countdown at the start
countdown = 0?
The sprite says
1
5
false
5
2
4
false
4
3
3
false
3
4
2
false
2
5
1
false
1
end
0
true
Go!
The countdown program says the numbers 5, 4, 3, 2 and 1, and then says Go!. The condition is checked before each turn. When countdown reaches 0, the loop ends, so 0 is never said.
In a farm game the player earns 2 coins every 5 seconds. A forever loop waits 5 seconds and adds 2 to the variable coins, again and again. The monitor on the stage shows the number going up: 2, 4, 6 and so on.
Count up in steps by changing the number that is added. The blocks below say 0, 3 and 6. Then the counter becomes 9, which is more than 8, so the loop stops.
Class activity
Human variable (no computer): one person holds a number card and is the variable countdown. An adult is the program. The adult says ‘set countdown to 6’, and the card shows 6. The adult says ‘say countdown’, and you say the number. Then ‘change countdown by minus 1’. Change the card. Keep going until the card shows 0, and then say ‘Go!’. Next, try counting up by 2 until the card shows more than 9. What numbers did you say? If you have a computer or tablet, build the countdown in Scratch and use the monitor.
Practice
1
Trace the blocks. What is the value of count at the end, and how many times does the loop run?
count goes 12, 8, 4. The loop runs 2 times, and at 4 the condition (count) < (5) is true, so it stops. The final value is 4.
2
Which block must go in the gap so that this loop can finish?
A block that changes the variable, such as change [countdown v] by (-1). Without it, countdown never changes and the loop never ends.
3
The farm game adds 2 coins every 5 seconds. About how many coins does the monitor show after 30 seconds?
About 12. In 30 seconds the loop runs 30 ÷ 5 = 6 times, and 6 × 2 = 12.
4
Ben forgets the change block in his countdown. Describe what happens and name this kind of bug.
The sprite keeps saying the same number, and the loop never ends because countdown never reaches 0. It is an infinite loop.