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.
Using a trace table
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)
| i | total |
|---|---|
| 0 | |
| 1 | 1 |
| 2 | 3 |
| 3 | 6 |
| 4 | 10 |
Three types of error
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
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.
Trace this code and give the output.
count = 0
for letter in "banana":
if letter == "a":
count = count + 1
print(count)
- The loop visits b, a, n, a, n, a.
- count goes up on each 'a': 1, 2, 3.
Answer: 3
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.
- 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 →
Try two quick questions
See if it has sunk in. Answers are marked straight away.
2 more questions
Try each one, then tap to check.
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.
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.
- Pearson Edexcel GCSE Computer Science (1CP2) specification, topic 1.2.4, 1.2.5, 6.1.5
- Pearson Edexcel International GCSE Computer Science (4CP0) specification, topic 1.1.4, 1.1.5, 2.1.3, 2.1.5, 2.1.6
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? →