Trace tables and types of error

A trace table lets you run a program in your head, one line at a time. It is also the best tool for hunting down logic errors.

Practise this lesson free (7 questions)

More tools

Using a trace table

Make a column for each variable (and one for output).
Each time a value changes, write the new value on a new row.
total = 0
for i in range(1, 5):
    total = total + i
print(total)
range(1, 5) gives 1, 2, 3, 4. Output: 10
itotal
0
11
23
36
410

Three types of error

Syntax error: the code breaks the rules of the language, so it cannot be translated or run. Example: print("Hi" with a missing bracket.
Runtime error: the program starts but crashes while running. Example: dividing by zero, or using a list index that does not exist.
Logic error: the program runs but gives the wrong result. Example: using < instead of <=.

Finding logic errors

Logic errors give no error message, so you must test.
Predict the correct answer, trace the code, and find the line where the trace stops matching your prediction.
A common cause is an off-by-one error in a loop range.
Worked example

Trace this code and give the output.
count = 0
for letter in "banana":
    if letter == "a":
        count = count + 1
print(count)

  1. The loop visits b, a, n, a, n, a.
  2. count goes up on each 'a': 1, 2, 3.

Answer: 3

Key idea

Trace tables track each variable line by line. Syntax errors stop code running, runtime errors crash it while running, and logic errors give wrong results with no message.

Key words
Trace table
A table that tracks the value of each variable as an algorithm runs.
Variable
A named store whose value can change while the program runs.
Syntax error
Code that breaks the rules of the language, so it cannot be translated or run.
Runtime error
An error that makes a program crash while it is running, such as dividing by zero.
Logic error
An error that lets a program run but gives the wrong result.

All GCSE and International GCSE Computer Science key terms and definitions →

Quick check

Try two quick questions

See if it has sunk in. Answers are marked straight away.

Extra practice

2 more questions

Try each one, then tap to check.

  1. What does this code print?
    x = 9
    y = 3
    while x > 0:
    x = x - y
    y = y + 1
    print(x)

    Show the answer

    Answer: -3

    x: 9 → 6 (y becomes 4) → 2 (y becomes 5) → −3. Now x > 0 is false, so -3 is printed.

  2. What does this code print?
    total = 0
    for i in range(3, 8):
    total = total + i
    print(total)

    Show the answer

    Answer: 25

    range(3, 8) gives 3, 4, 5, 6, 7. 3 + 4 + 5 + 6 + 7 = 25.

Check you have got it

Answer 7 quick questions with instant marking. If you get one wrong, GCSE-ready shows you why and gives you another go. It is free, and you do not need an account.

Video for this lesson coming soon. Subscribe on YouTube

Sources and references

Facts checked against the sources below, 17 October 2026. Last updated 2 October 2026. Spotted a mistake? Tell us and we will fix it.

What do exam command words like explain and evaluate mean? →

Related lessons