Open the app
  1. Home
  2. Lessons
  3. GCSE & IGCSE Computer Science
  4. Trace tables and types of error

Trace tables and types of error

🎬 The doodle video for this lesson is coming soon. Subscribe on YouTube to see it first.

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.

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)

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.

The interactive lesson includes the diagrams for this topic.

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.