A trace table is a method of manually working through an algorithm step by step, recording the value of each variable after every instruction. It is used to check correctness and identify bugs in algorithms.
Create a column for each variable and a column for any output
Fill in each row as you execute each line of the algorithm in order
Very common in Edexcel Paper 1 exam questions — you must be able to complete these
Worked Trace Table Example
Trace this algorithm:
x ← 1 total ← 0 WHILE x <= 3 total ← total + x x ← x + 1 END WHILE OUTPUT total
x
total
Output
1
0
2
1
3
3
4
6
6
Using Trace Tables to Find Bugs
Spotting Logic Errors
If the output from your trace table doesn't match the expected output, the algorithm has a logic error. Look back through the table to find which row produced an unexpected value — that is where the bug is located.
Check conditions carefully — does < mean <= is needed?
Check initialisations — is the variable set to 0 or 1 at the start?
Check increment — is the counter increasing correctly?
Exam Practice
Have a go at this question
Edexcel-style question
Complete a trace table for the following algorithm with the given input. What is the output?
n ← 4, result ← 1 FOR i ← 1 TO n result ← result * i END FOR OUTPUT result