Cambridge IGCSE Computer Science (0478) · lesson 26 of 35 · 25 min
What Is a Trace Table?
A trace table (dry run) is a technique for manually tracking the values of variables as an algorithm executes, step by step. It is used to verify that an algorithm produces the correct output and to identify logic errors (bugs).
Each column represents a variable or output; each row represents one step of execution.
Worked Example
Trace the following algorithm for inputs 4, 7, 2
total ← 0
count ← 0
FOR i ← 1 TO 3
INPUT num
total ← total + num
count ← count + 1
NEXT i
OUTPUT total / count
Finding Errors with Trace Tables
Trace tables reveal three types of errors:
- Logic errors: Wrong output despite no crash (e.g. off-by-one in loop bounds).
- Infinite loops: Loop condition never becomes false.
- Wrong variable updates: Variable not initialised or updated incorrectly.
What is the purpose of a trace table?
- To compile code faster
- To manually track variable values through an algorithm
- To measure execution time
- To format pseudocode
Answer: To manually track variable values through an algorithm
A trace table tracks variable values step by step to verify correctness.
In a trace table, each row typically represents:
- A variable
- One step of execution
- One input value
- One output value
- Answer: 1
Each row records the state of variables after one step of the algorithm. A trace table shows a variable never changes from its initial value. This suggests:
- The algorithm is correct
- The variable is never updated — likely a logic error
- The variable is a constant
- The loop ran zero times
- Answer: 1
If a variable that should change never does, there is likely a missing update statement.
Previous lesson: Pseudocode conventions and flowcharts
Back to Cambridge IGCSE Computer Science (0478)
Next lesson: Linear search and binary search