Validation and Testing · Cambridge syllabus 7.5, 7.6, 7.7, 7.8 · Lesson 3 of 3
Trace Tables
A trace table records variable values as execution proceeds. Follow assignment, selection and repetition in order, updating only the values changed by each step. Record output at the point it occurs.
By the end, you can
- Trace variables through an algorithm.
- Record values after each step.
- Identify final outputs.
- Detect logical errors using trace tables.
Algorithm tracing
A trace table is a record of how the values in an algorithm change as it runs. It has one column for each variable, plus columns for any input and output, and one row for each step that changes something.
It is used to work out what an algorithm actually does, as opposed to what it was meant to do, and it is the standard way to find a logic error — a program that runs without crashing and produces the wrong answer.
The method is mechanical and must stay so. Draw the columns; work through the algorithm one statement at a time with a chosen set of inputs; write a new row whenever a variable changes; and never skip a loop iteration, because the mistake is usually in the last one. When an output statement is reached, record what it prints.
On a loop, each pass produces its own rows, and the condition is evaluated at the top or the bottom depending on whether it is a WHILE or a REPEAT — which is why a REPEAT always executes its body at least once and a WHILE need not execute it at all.
The errors a trace exposes are the ones that are invisible in the code: a total that was never reset to zero, a counter increased in the wrong place, a loop running one time too many or too few, a condition using > where >= belongs, and a value that turns out to be exactly what was expected at every step except one.
Worth remembering
- One column per variable, plus input and output; one row per change.
- Work through statement by statement with chosen inputs, skipping no iteration.
- It is how a logic error is found, when the program runs but is wrong.
Worked example
For Total ← 0 followed by adding 3, 4 and 5, the trace gives totals 0, 3, 7 and 12.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
After the assignments, Y = 5 and X = 10.
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.