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.
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.
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.
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.
coins starts at 9. 9 > 5, so coins becomes 5. Then 5 < 6 is true, so coins becomes 15. It prints 15. A quick prediction of 5 forgets the second if.
2
Complete the trace table for the same program. What goes in the empty cells?
Line run
coins
1
?
3
?
5
?
The rows are: line 1, coins 9; line 3, coins 5; line 5, coins 15. Line 5 is the line that adds 10, because line 4 is the second if.
3
Change the second if in the coin program to elif. What is printed now? Explain the difference.
It prints 5. After the first if, coins is 5, and an elif is skipped when the first branch ran. With two separate ifs both conditions are checked.
4
Explain why a prediction can be wrong even when the programmer is careful, and what tracing adds.
A prediction is quick, and it is easy to miss a branch or a changed value. Tracing records every change step by step, so the result is worked out, not guessed.