Tracing an algorithm means following it step by step, exactly as a computer would, and recording what happens to each variable. It is a skill that shows up in exam questions, and it is also the most reliable way to find logic errors in your own programs. This article shows a simple method and two worked examples.
What a logic error is
A syntax error stops a program from running at all. A logic error is more awkward: the program runs but gives the wrong result. Because there is no error message, the only way to find it is to check what the code really does against what you intended.
The tracing method
- List the variables. Include any loop counters.
- Make a column for each one, plus a column for output if the algorithm prints anything.
- Work one line at a time. Update the variables in the order the code does it.
- Do not skip steps or guess. If you are tempted to "see" the answer, write the next row anyway.
- Compare the final state with what you expected.
Worked example 1: a loop with a running total
Here is a short program. Python-style syntax is used, but the method is the same for the pseudocode in exams.
total = 0
for i in range(1, 5):
total = total + i * 2
print(total)The call range(1, 5) produces 1, 2, 3 and 4, and stops before 5. The trace table is:
i total
- 0
1 2
2 6
3 12
4 20The program prints 20. A common slip is to include 5 in the loop, which would give 30 instead. Writing the table avoids this because you list every value of i explicitly.
Worked example 2: a classic logic error
A student writes a function to find the largest number in a list:
def find_largest(numbers):
largest = 0
for n in numbers:
if n > largest:
largest = n
return largestIt works for [4, 9, 2], so it looks fine. Now trace it with [-7, -3, -9]:
n n > largest? largest
- 0
-7 No 0
-3 No 0
-9 No 0The function returns 0, which is not even in the list. The correct answer is −3. The error is in the starting value: the function assumes all numbers are positive. The fix is to start with the first item in the list:
def find_largest(numbers):
largest = numbers[0]
for n in numbers:
if n > largest:
largest = n
return largestTraced again with [-7, -3, -9], largest begins as −7, becomes −3 and stays there. The result is correct.
Test data that finds bugs
Tracing is easier when you choose revealing inputs. For every algorithm, try:
- Normal data: the ordinary case you expect.
- Boundary data: the smallest and largest values allowed, or the first and last item in a list.
- Erroneous data: an empty list, text instead of a number, a negative value where only positives were expected.
The negative numbers in the example above were a form of boundary or unusual data. They exposed a bug that the first test missed.
Common sources of logic errors
- Off-by-one errors: a loop that stops one step early or runs one step too far.
- Wrong starting value: like the maximum finder, or starting a counter at 1 when it should be 0.
- Comparing the wrong way round: using > instead of >=, or the opposite.
- Variables updated in the wrong order: swapping two values without a temporary variable.
- Loops that never end: a while loop where the condition never changes.
How to practise
- Trace short algorithms by hand, then run them to check your trace.
- Predict before you run. Write down the expected output, then compare.
- Break working code on purpose. Change one line and predict what goes wrong.
- Swap with a friend. Ask them to trace your code without running it.
- Practise past-paper trace-table questions under timed conditions.
A short checklist
- I list every variable before I start tracing.
- I update one line at a time and never skip rows.
- I test with normal, boundary and erroneous data.
- I predict the output before running code.
- I can explain why a bug happened, not just that I fixed it.
Next steps
Tracing is a skill that gets faster with feedback. If you would like help, read about Computer Science lessons, find out more about Safin, or send an enquiry. For a topic that builds on algorithm tracing, read understanding Big O notation.

