← Back to GCSE guidesCodeBash GCSE Computer Science← PreviousNext →
Algorithms & Programming

Trace Tables

A trace table tracks what every variable holds, line by line, as a program runs, without actually running it. It's the closest thing to reading a program's mind, and it's the single most reliable way to find a bug before you've even typed the code into a computer.

Section 2

Tracing a loop

This program adds up the numbers 1 to 5. Step through it and watch the trace table build itself, one row per line executed.

Trace table

LineitotalOutput

Code

Controls

Section 3

Tracing selection (if / elif / else)

Loops aren't the only thing worth tracing. Here's a program that branches differently depending on the value of i, a classic Fizz/Buzz-style test of whether you can follow conditional logic by hand.

Trace table

LineiOutput

Code

Controls

Section 4

Using a trace table to find a bug

This is where trace tables earn their keep. Here's a program meant to swap the values of a and b. Trace it through and watch exactly where it goes wrong, then compare it to the fixed version.

Trace table

Lineabtemp

Code

Controls

Exam tips

  • Add a new row every time a variable changes, or a line produces output. Some exam boards want a row per iteration instead: check your specification's example answers.
  • Watch loop boundaries carefully: "for i = 1 to 5" almost always means 5 iterations inclusive of both ends. Off-by-one mistakes are the single most common trace table error.
  • A swap needs a temporary variable to hold one value while the other is overwritten, otherwise the first value is lost before it's used.
  • If a question gives you a program and asks for the final output, tracing it through by hand is far more reliable than guessing.
Section 5

Check your understanding