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

Counting and asking

Week 10 · Unit 4: Be a storyteller · 3 lessons
1

A counter that reacts

Computing 5: Unit 4, pp. 44–57
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.

Bakerwhenclickedsetloavesto0sayLet’s bake!for2secondsBakerwhenspacekey pressedchangeloavesby1sayTray full!for2secondsifloaves=5then
Worked examples
StepInstruction
1Start program when the green flag is clicked
2Set loaves to 0
3Say ‘Let’s bake!’ for 2 seconds
4Start program when the space key is pressed
5Change loaves by 1
6If loaves = 5, say ‘Tray full!’ for 2 seconds
This is the algorithm for the same program. Each step matches one block.
Space pressesloavesSays ‘Tray full!’?
11no
22no
33no
44no
55yes
66no
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.
Bakerwhenclickedsetloavesto0sayLet’s bake!for2secondsBakerwhenspacekey pressedchangeloavesby1sayTray full!for2secondsifloaves>4then
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’?
2
A pupil’s tray never says ‘Tray full!’ even after 10 presses. Find the bug.
whenspacekey pressedsetloavesto0changeloavesby1sayTray full!for2secondsifloaves=5then
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!’.
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.
(a) pages: ?(b) seconds: ?(c) laps: ?