You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers how trace tables are used as a debugging tool, as required by OCR J277 Section 2.4. While trace tables were introduced in the Algorithms section for following algorithm execution, this lesson focuses specifically on using trace tables to find and fix bugs — particularly logic errors — in programs.
Logic errors do not cause error messages — the program runs but produces the wrong output. Trace tables allow you to systematically track variable values at each step, making it possible to pinpoint exactly where the logic goes wrong.
The debugging process with trace tables:
OCR Exam Tip: Trace table questions for debugging will typically show code with a logic error and ask you to complete the trace table, identify the error, and suggest a fix. These questions are worth 4–6 marks and test multiple skills at once.
Buggy code — supposed to calculate the sum of numbers from 1 to 5:
total = 0
for i = 1 to 4
total = total + i
next i
print(total)
Trace table:
| Step | i | total | Output |
|---|---|---|---|
| Init | - | 0 | |
| 1 | 1 | 1 | |
| 2 | 2 | 3 | |
| 3 | 3 | 6 | |
| 4 | 4 | 10 | 10 |
Expected output: 15 (1+2+3+4+5=15) Actual output: 10
The bug: The loop goes from 1 to 4 instead of 1 to 5. The number 5 is never added.
The fix: Change for i = 1 to 4 to for i = 1 to 5.
Buggy code — supposed to calculate the average of three scores:
score1 = 80
score2 = 70
score3 = 90
total = score1 + score2 + score3
average = total * 3
print("Average: " + str(average))
Trace table:
| Step | score1 | score2 | score3 | total | average | Output |
|---|---|---|---|---|---|---|
| Init | 80 | 70 | 90 | - | - | |
| 1 | 80 | 70 | 90 | 240 | - | |
| 2 | 80 | 70 | 90 | 240 | 720 | "Average: 720" |
Expected output: "Average: 80" Actual output: "Average: 720"
The bug: average = total * 3 should be average = total / 3. Multiplication instead of division.
The fix: Change * to /.
Buggy code — linear search that should find and count occurrences:
data = [3, 7, 3, 5, 3, 8]
target = 3
count = 1
for i = 0 to data.length - 1
if data[i] == target then
count = count + 1
endif
next i
print("Found " + str(count) + " times")
Trace table:
| i | data[i] | data[i]==3? | count |
|---|---|---|---|
| - | - | - | 1 |
| 0 | 3 | Yes | 2 |
| 1 | 7 | No | 2 |
| 2 | 3 | Yes | 3 |
| 3 | 5 | No | 3 |
| 4 | 3 | Yes | 4 |
| 5 | 8 | No | 4 |
Expected output: "Found 3 times" Actual output: "Found 4 times"
The bug: count is initialised to 1 instead of 0, so the count is always one too many.
The fix: Change count = 1 to count = 0.
Buggy code — supposed to find the largest value in an array:
values = [3, 8, 1, 5, 2]
largest = values[0]
i = 0
while i < values.length
if values[i] > largest then
largest = values[i]
endif
i = i + 1
endwhile
print("Largest: " + str(largest))
Trace table:
| i | values[i] | values[i] > largest? | largest |
|---|---|---|---|
| 0 | 3 | 3 > 3? No | 3 |
| 1 | 8 | 8 > 3? Yes | 8 |
| 2 | 1 | 1 > 8? No | 8 |
| 3 | 5 | 5 > 8? No | 8 |
| 4 | 2 | 2 > 8? No | 8 |
Output: "Largest: 8" — this is correct!
This example shows that trace tables can also confirm that code works correctly. If the output matches the expected result, no fix is needed.
OCR Exam Tip: Even if a trace table shows the code is correct, completing it accurately still earns marks. Do not skip steps or assume the answer — work through every step systematically.
| Step | Action |
|---|---|
| 1 | Run the program with known test data |
| 2 | Compare actual output with expected output |
| 3 | If different, create a trace table for the relevant section |
| 4 | Work through line by line, recording all variable values |
| 5 | Find the first point where a value is unexpected |
| 6 | Examine the line of code responsible for that value |
| 7 | Identify and fix the error |
| 8 | Re-test with the same data to verify the fix |
| 9 | Test with additional data to check for other errors |
flowchart TD
A[Run program with known data] --> B{Output matches expected?}
B -- Yes --> Z[No bug — move on]
B -- No --> C[Build trace table: variable per column]
C --> D[Walk code line by line, fill values]
D --> E[Find first row where value diverges]
E --> F[Inspect the line that produced it]
F --> G[Hypothesise + fix the logic error]
G --> H[Re-trace with same data]
H --> I{Now matches expected?}
I -- No --> E
I -- Yes --> J[Regression test other inputs]
| Tip | Why It Matters |
|---|---|
| Include all variables | Missing a variable can hide the bug |
| Work line by line | Do not skip steps or make assumptions |
| Check loop conditions carefully | Off-by-one errors are very common |
| Record every change | Even if a value does not change, noting it helps |
| Include an output column | Always record what print() produces |
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.