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

Plan before you code

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

Build and test with a variable

Computing 5: Unit 4, pp. 44–57
GoalBuild a program that counts with a variable and test it with a table of expected and actual results.
New words
test · to run a program and see whether it does what it shouldexpected result · what we think the program should do, worked out before we run itactual result · what the program really does when we run itdebug · to find a bug and fix it
Explanation

To build a counting program, follow four steps. First, make a variable and give it a clear name. Second, set it to 0 when the green flag is clicked. Third, put a ‘change’ block under the event that should be counted, such as a click on a sprite. Fourth, test the program. Before each test, work out the expected result. Then run the program and write down the actual result. If the two are different, there is a bug. The cause is often a wrong number, a ‘set’ block where a ‘change’ block should be, or a different variable chosen from the list. Showing the variable on the stage lets us watch its value.

Bellwhenclickedsetringsto0Bellwhen this sprite clickedstart soundPopchangeringsby1
Worked examples
TestExpected ringsActual rings
flag, no clicks00
flag, then 5 clicks55
5 clicks, then the flag again00
A test table compares expected and actual results. The bell adds 1 to rings for each click. Both columns match, so these three tests pass.
when this sprite clickedstart soundPopsetringsto1
Bug 1: the script uses ‘set’ and not ‘change’. Each click sets rings to 1 again. After 5 clicks the value is still 1, but the expected result was 5.
when this sprite clickedchangeclicksby1
Bug 2: the script changes a different variable. After 5 clicks the variable rings shows 0, because the click script adds to clicks.
Class activity

Bug-hunt cards (pairs). Write six small programs on cards, each with one ‘set’ block, one ‘change’ block and a claim such as ‘after 3 clicks the value is 6’. Make some claims true and some false. Swap cards. For each card, work out the expected result by tracing it with a cup and counters, and say whether the claim is true. If it is false, find the bug. Who found the most bugs? If you have a computer or tablet, build the bell program and test it with a table.

Practice
1
The gong adds 2 to hits each time that it is clicked. What is the value of hits after 7 clicks? What is it after the flag is clicked again?
Gongwhenclickedsethitsto0Gongwhen this sprite clickedchangehitsby2
2
Complete the test table for a script that adds 4 for each click.
ClicksExpected value
0?
2?
5?
3
A pupil expects 10 clicks to give 10 but sees 30. Which block has the wrong number?
when this sprite clickedchangeringsby3
4
Name two things to check when the value does not change at all after clicking the sprite.