A trace table debugs a program by recording how variables, conditions, and outputs change as each instruction executes. Compare the recorded values with the expected results; the first difference usually identifies where the error occurs.
How the process works
Tracing is a manual dry run of an algorithm. Follow these steps:
- Identify every variable, condition, and output that may change.
- Create one column for each item.
- Use the given test data to execute the algorithm one statement at a time.
- Add a row whenever a value changes or a condition is evaluated.
- Compare the final and intermediate values with the expected results.
For example, trace this pseudocode:
x = 2
total = 0
WHILE x <= 4
total = total + x
x = x + 1
END WHILE
OUTPUT total
| Execution point | x | total | x <= 4 | output |
|---|---|---|---|---|
| Initial values | 2 | 0 | TRUE | : |
| After iteration 1 | 3 | 2 | TRUE | : |
| After iteration 2 | 4 | 5 | TRUE | : |
| After iteration 3 | 5 | 9 | FALSE | : |
| OUTPUT executes | 5 | 9 | FALSE | 9 |
The table confirms that the loop adds 2, 3, and 4, producing 9. If the expected result were 9 but the trace produced 6, you would inspect the first row where the values diverged. This can expose a logic error, such as an incorrect update, condition, or variable initialization.
A common misconception is that a trace table records only the final output. It must show intermediate states in execution order; otherwise, it cannot reveal where the program begins behaving incorrectly.
IB exam technique
For B2.1 Programming fundamentals, exam questions may ask you to construct or complete a trace table. Show every variable update, evaluate Boolean conditions accurately, and stop loops at the correct point. Use the trace to explain the error rather than merely stating that the output is wrong.