A trace table records the value of each variable as an algorithm runs. To connect it with the output, find the variable named in the OUTPUT statement and read its value from the row where the program reaches that statement. Then explain why the loop ended there.
This lesson belongs to the problem-solving explanations module. It builds on explaining the purpose of a variable, since you need to know which variable is the answer, and on tracing from algorithm design.
How do you link the table to the output?
- Build the table with one column per variable and one row per update.
- Mark the row where the loop test fails. The loop ends at the first test that is false.
- Check the OUTPUT statement. Note which variable it names.
- Read that variable’s final value from the last row that changed it.
- Write one sentence saying which variable, what value, and why the loop stopped.
Worked example
Total ← 0
Number ← 10
WHILE Number > 0 DO
Total ← Total + Number
Number ← Number - 3
ENDWHILE
OUTPUT Total
| Pass | Number > 0 ? | Total after | Number after |
|---|---|---|---|
| Start | 0 | 10 | |
| 1 | 10 > 0 true | 10 | 7 |
| 2 | 7 > 0 true | 17 | 4 |
| 3 | 4 > 0 true | 21 | 1 |
| 4 | 1 > 0 true | 22 | −2 |
| Test | −2 > 0 false | 22 | −2 |
Explanation. The loop adds 10, 7, 4 and 1 to Total, giving 10 + 7 + 4 + 1 = 22. After pass 4, Number is −2, so the test −2 > 0 is false and the loop ends. The value −2 is never added. The statement OUTPUT Total prints 22.
Double check. The numbers added are 10, 7, 4, 1 (each 3 less than the one before, while still above 0). Their sum is 22.
The mistake to watch for
A common slip is to read the wrong column or the wrong row:
The output is −2 because that is the last value in the table.
−2 is the last value of Number, but the OUTPUT statement names Total. A second version of the slip is giving 21 or 20 by stopping the trace one pass early or adding −2 by mistake.
The correction: look at the statement first. It says OUTPUT Total, so read the Total column. Its last value is 22.
Check yourself
1. Trace the code and state the output with one sentence of explanation.
A ← 2
B ← 1
REPEAT
B ← B * A
A ← A + 1
UNTIL A > 4
OUTPUT B
Show answer
Start A = 2, B = 1. Pass 1: B = 1 × 2 = 2, A = 3, test 3 > 4 false. Pass 2: B = 2 × 3 = 6, A = 4, test 4 > 4 false. Pass 3: B = 6 × 4 = 24, A = 5, test 5 > 4 true so the loop stops. The output is 24, because B was last updated in pass 3 and OUTPUT B prints that value.
2. In the worked example, which row of the table gives the output, and why not the Test row?
Show answer
Pass 4 is the last row that changes Total, giving 22. The Test row only checks the condition, and the loop body is not run, so Total stays at 22. Both rows show 22 in the Total column, but pass 4 is where the value was produced.
3. Change the starting value of Number in the worked example to 9. What is the output?
Show answer
Number runs 9, 6, 3 and then 0. Total becomes 9, 15, 18. After pass 3, Number is 0, and 0 > 0 is false, so the loop stops. The output is 18 (9 + 6 + 3 = 18).
Where this leads next
The last skill in this module is shrinking a failing program to its smallest form in preparing a minimal reproducible learning example. The restricted pseudocode trace trainer lets you check your own table against a stepped trace.
If your tables are correct but the explanation of the output loses marks, our teachers can practise the final sentence with you in online one-to-one Computer Science tuition.