← OCR GCSE (J277) hub

Trace Tables

Learn to dry-run an algorithm one line at a time, watch a full example being traced, then complete the challenges yourself.

What is a trace table?

A trace table is a record of what happens inside a program as it runs, line by line. You pretend to be the computer: you execute each statement by hand and write down the value of every variable each time it changes, the result of every condition you test, and anything that is output.

Each row is one moment in time (one line being executed). Each column is one variable, one condition, or the output. Reading down a column shows you how a variable's value evolves; reading across a row shows the whole state of the program at that instant.

Why bother? Trace tables are how you dry-run code: you find logic errors without a computer, you prove to yourself what a loop really does, and in the OCR exam you will be asked to "complete the trace table" for a given algorithm — a very common question worth several marks.

A tiny example

Trace this program. There are two variables, a and b, and one output.

Algorithm
1a = 4
2b = a * 3
3a = a + b
4print(a)

Its trace table

LineabOutput
14
212
316
416

Notice: on line 2, a has not changed, so its cell is left blank. A blank cell means "same value as the row above".

The rules we use on this page

  1. One row per line executed that changes a variable, tests a condition, or produces output. Lines like next i, endwhile, else and endif do nothing on their own, so they get no row.
  2. Only write a value when it changes. Leave the cell blank if the variable keeps its value. (Writing the unchanged value again is not wrong — it is just slower.)
  3. A for line is where the loop variable gets its value. Each time the loop comes round, record for i = 1 to 4 as a row with the new value of i.
  4. Conditions (while, if) get their own column. Write True or False each time the condition is tested — including the final test that makes a while loop stop.
  5. Output goes in the last column, exactly as it would appear on screen. Text output is written without quotes.
  6. Use the current values. In y = x - y, use the value of x as it is now — if an earlier line changed it, use the new value.
Most common mistakes: stopping a while loop one iteration too early or too late (always test the condition one last time); using an old value of a variable after it has been reassigned; writing "even" with quotes in the output column; and forgetting that MOD gives the remainder (7 MOD 2 = 1) while DIV gives the whole-number quotient (7 DIV 2 = 3).

Ready? Head to the to see a full loop traced step by step.

Worked example: tracing a loop

Press Next step to execute one line at a time. The yellow pointer shows the line being run; the table fills in as we go, and the box underneath explains the decision being made.

Algorithm
Step 0 of 10
LinetotaliOutput
Start. Nothing has run yet, so the table is empty. We begin at line 1.

What to take away

  • The for line appeared three times in the Line column — once for each value of i. The next i line never appeared.
  • total only got a cell when line 3 actually changed it.
  • The output came last and went in its own column, with no quotes.

Now try it yourself:

Algorithm

Tip: type a line number in the Line column and the pointer will jump to that line. Press Tab to move across the row.

Blank cells mean "unchanged from the row above". Write True/False for conditions. Output text goes without quotes.