2
Stories, tests and bugs
Computing 2: Units 9–11, pp. 122–163
GoalRemember how to predict the output, build in parts, test and debug.
New words
output · the result an algorithm givesbug · a mistake in a programtest · to run a program to see if it works
Explanation
An algorithm can take an input and give an output. We predict the output by doing each step in order. We build a program in small parts and test each part. If a program does not match its algorithm, it has a bug. We compare, find the block that is different, fix one bug and test again. Characters can send and receive messages, and the colours must match.
input: 6→double→take away 2→output: 10
Worked examples
| After step | Number |
|---|---|
| enter the number | 3 |
| add 4 | 7 |
| double | 14 |
| take away 5 | 9 |
The algorithm is: start on bump, move left 2, say “Oh!”, stop. The red outline shows the bug: the move block goes right. Change it to left 2.
The frog sends a green message. The duck starts on a green message. The colours match, so the duck starts when the frog finishes.
Class activity
Predict and check relay (three players): player 1 chooses an input number and writes an algorithm of three steps. Player 2 predicts the output on paper. Player 3 checks with counters. Was the prediction right? If not, find the step with the mistake. Swap roles and play again.
Practice
1
The input is 2. The steps are: double, add 5, take away 3. What is the output?
input: 2→double→add 5→take away 3→output: ?
2 becomes 4, then 9, then 6. The output is 6.
2
The algorithm is: start on tap, move down 2, hop 3, stop. Which block has a bug and how do you fix it?
The hop block says 2. Change it to 3.
3
The cat sends a blue message, but the mouse never starts. The mouse starts on a red message. What is the fix?
The colours do not match. Change the mouse’s first block to a blue message.
4
Why do we test each part of a program as we write it?
We find errors early, and they are easy to fix because they are in the part we just wrote.