GoalPlan and trace a program that counts with a variable and reacts when the count reaches a number.
New words
counter · a variable that counts how many times something happenscondition · a question that is true or false, such as ‘loaves = 5’algorithm · a list of steps, in order, that solves a problemtrace · to follow a program step by step to see what it does
Explanation
A counter is a variable that counts events. Here the Baker puts a loaf on a tray each time the space key is pressed. The plan has a purpose: to count loaves and say when the tray is full. It has two events, the flag and the space key. When the flag is clicked, the counter is set to 0. When the space key is pressed, the counter goes up by 1, and then an ‘if’ block checks the count. If the count is exactly 5, the Baker says ‘Tray full!’. We must trace the program to check it. The sign = means ‘exactly equal’, so a count of 6 does not make the condition true.
Worked examples
Step
Instruction
1
Start program when the green flag is clicked
2
Set loaves to 0
3
Say ‘Let’s bake!’ for 2 seconds
4
Start program when the space key is pressed
5
Change loaves by 1
6
If loaves = 5, say ‘Tray full!’ for 2 seconds
This is the algorithm for the same program. Each step matches one block.
Space presses
loaves
Says ‘Tray full!’?
1
1
no
2
2
no
3
3
no
4
4
no
5
5
yes
6
6
no
A trace table shows the value after each press and whether the Baker speaks. The Baker speaks only when loaves is exactly 5. At press 6 the count is 6, so the condition is false.
Change the condition to ‘loaves > 4’ and the Baker speaks at press 5 and also at every later press, because 5, 6, 7 and so on are all bigger than 4.
Class activity
Tray game (2 players, a plate and paper ‘loaves’). Write the Baker’s program on cards. One player acts as the computer and follows the cards exactly. When the other player taps the table (the space key), the computer puts a loaf on the plate, counts aloud and checks the card. The computer says ‘Tray full!’ only when the count is exactly 5. Tap 7 times. When did the computer speak? Now change the card to ‘more than 4’ and play again. What changed?
Practice
1
Use the Baker program from the lesson, with ‘loaves = 5’. The space key is pressed 7 times. How many times does the Baker say ‘Tray full!’? What if the condition is ‘loaves > 4’?
With ‘= 5’ the Baker speaks once, at press 5. With ‘> 4’ the Baker speaks at presses 5, 6 and 7, which is 3 times.
2
A pupil’s tray never says ‘Tray full!’ even after 10 presses. Find the bug.
The ‘set loaves to 0’ block is inside the space key script, so it resets the count to 0 each time and the ‘change’ block makes it 1. The count never reaches 5. The ‘set’ block must be in the flag script.
3
Write an algorithm for a sprite that collects stars. The variable stars starts at 0. When the sprite is clicked, stars goes up by 2. When stars is exactly 10, the sprite says ‘You win!’.
1 Start program when the green flag is clicked. 2 Set stars to 0. 3 Start program when this sprite is clicked. 4 Change stars by 2. 5 If stars = 10, say ‘You win!’ for 2 seconds. It speaks at the fifth click, because 5 × 2 = 10.
4
Choose a clear name for a variable that counts (a) the pages that you have read, (b) the seconds that are left in a game, (c) the laps that a runner has finished.
For example: (a) pages read, (b) seconds left, (c) laps. Each name tells what the variable counts.