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

Test it properly

Week 14 · 1.7 Software development · 3 lessons
1

Guess first, then check

Computing 8: Topic 1.7, pp. 150–172
GoalPredict what a program will do, then trace it to check, and explain any difference.
New words
prediction · working out what a program will do before running or tracing ittracing · following a program line by line and writing down how the values changetrace table · a table that records the value of each variable as the program runs
Explanation

Before a program is run, a programmer often makes a prediction. This is a quick idea of what it will do. A prediction is useful, but it can be wrong. To be sure, you trace the program. You follow the lines in order, write down the value of each variable after every change, and decide which branches run. Then you compare the traced result with your prediction. If they differ, find out why. Often a branch was missed, such as two separate if statements that both run. A trace table is neat and careful. Remember that a chain of if, elif and else runs only one branch, but two separate if statements are each checked on their own.

Line runstickers
14
36
612
Worked examples
Python
1stickers = 4
2if stickers > 3:
3    stickers = stickers + 2
4if stickers > 5:
5    stickers = stickers * 2
6print(stickers)
Output
12
A prediction that goes wrong. A pupil glances at the program and says “4 is greater than 3, so 4 + 2 = 6.” That forgets the second if. The program checks it separately: 6 is greater than 5, so it doubles. Tracing gives 12.
Pseudocode
1stickers ← 4
2IF stickers > 3 THEN
3    stickers ← stickers + 2
4ENDIF
5IF stickers > 5 THEN
6    stickers ← stickers * 2
7ENDIF
8OUTPUT stickers
Output
12
The same program as pseudocode. The table at the top of this lesson traces it: each row shows the line number that changes the value and the new value of stickers. Both IF conditions are tested, and both are true. Real pseudocode has no fixed rules, so other people may write it a little differently.
Python
1stickers = 4
2if stickers > 3:
3    stickers = stickers + 2
4elif stickers > 5:
5    stickers = stickers * 2
6print(stickers)
Output
6
A small change makes a big difference. Here the second test is an elif. Only the first true branch runs, so the second one is skipped. The output is 6, and the first prediction would have been right.
Class activity

Predict and trace (no computer). Write a short program on paper with two separate if statements, such as one about coins and one about stickers. Give it to a partner. First, your partner writes down a prediction only. Then your partner traces it with a table. Did the prediction match the trace? If not, which line was missed? Swap roles. Then change the second if to an elif and trace again. If you have a computer or tablet, ask an adult to help you run one of the programs to check your trace.

Practice
1
Predict the output of the program, then trace it with a table (line, coins) and give the printed number.
Python
1coins = 9
2if coins > 5:
3    coins = coins - 4
4if coins < 6:
5    coins = coins + 10
6print(coins)
2
Complete the trace table for the same program. What goes in the empty cells?
Line runcoins
1?
3?
5?
3
Change the second if in the coin program to elif. What is printed now? Explain the difference.
4
Explain why a prediction can be wrong even when the programmer is careful, and what tracing adds.