A trace table is a technique for manually tracking the values of variables as an algorithm executes step by step. It allows you to check what an algorithm does for a given input without running it on a computer — this is called dry-running or desk checking.
A trace table has one column per variable (plus an output column if needed) and one row per relevant step execution.
Trace the following algorithm with input n = 4:
| Step | count | result | count ≤ n? | Output |
|---|---|---|---|---|
| Init | 1 | 1 | — | — |
| Loop 1 | 2 | 1 | TRUE | — |
| Loop 2 | 3 | 2 | TRUE | — |
| Loop 3 | 4 | 6 | TRUE | — |
| Loop 4 | 5 | 24 | TRUE | — |
| End | 5 | 24 | FALSE | 24 |
The algorithm calculates n! (factorial). Output: 24
| Error Type | Description | Example |
|---|---|---|
| Syntax error | Code breaks the rules of the language — cannot be translated | Missing END WHILE; misspelled keyword |
| Logic error | Algorithm runs but produces the wrong result — error in the logic | Using < instead of ≤ in a loop condition |
| Runtime error | Algorithm crashes during execution due to an impossible operation | Dividing by zero; accessing an index that doesn't exist |
Trace tables help identify logic errors — the algorithm runs, but the trace shows the variable taking an unexpected value. Compare what the trace table shows against what the algorithm should produce to find where the logic went wrong.