You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
A trace table is a technique used to track the values of variables as an algorithm executes, step by step. Trace tables are an essential tool for testing, debugging, and understanding how an algorithm works.
Trace tables help you:
They are frequently tested in GCSE exams. You will often be given pseudocode or a flowchart and asked to complete a trace table.
The flow of values through code, captured by a trace table, looks like this:
graph TD
A["Read next line of code"] --> B{"Is it an assignment?"}
B -->|Yes| C["Update variable column"]
B -->|No| D{"Is it OUTPUT?"}
D -->|Yes| E["Add value to Output column"]
D -->|No| F{"Is it a loop/IF?"}
F -->|Yes| G["Evaluate condition"]
F -->|No| H["Carry values forward"]
C --> I["Move to next line"]
E --> I
G --> I
H --> I
x = 0
FOR i = 1 TO 5
x = x + i
NEXT i
OUTPUT x
Trace Table:
| Step | i | x | Output |
|---|---|---|---|
| Initial | — | 0 | |
| 1 | 1 | 1 | |
| 2 | 2 | 3 | |
| 3 | 3 | 6 | |
| 4 | 4 | 10 | |
| 5 | 5 | 15 | |
| End | — | 15 | 15 |
The algorithm calculates the sum of numbers from 1 to 5 (1 + 2 + 3 + 4 + 5 = 15).
total = 0
count = 0
numbers = [4, 7, 2, 9, 1]
FOR i = 0 TO 4
IF numbers[i] > 3 THEN
total = total + numbers[i]
count = count + 1
ENDIF
NEXT i
OUTPUT total
OUTPUT count
Trace Table:
| Step | i | numbers[i] | numbers[i] > 3? | total | count | Output |
|---|---|---|---|---|---|---|
| Initial | — | — | — | 0 | 0 | |
| 1 | 0 | 4 | Yes | 4 | 1 | |
| 2 | 1 | 7 | Yes | 11 | 2 | |
| 3 | 2 | 2 | No | 11 | 2 | |
| 4 | 3 | 9 | Yes | 20 | 3 | |
| 5 | 4 | 1 | No | 20 | 3 | |
| End | — | — | — | 20 | 3 | 20, 3 |
The algorithm finds the total and count of numbers greater than 3.
n = 100
count = 0
WHILE n > 1
n = n DIV 2
count = count + 1
ENDWHILE
OUTPUT count
Trace Table:
| Step | n | n > 1? | count | Output |
|---|---|---|---|---|
| Initial | 100 | — | 0 | |
| 1 | 50 | Yes | 1 | |
| 2 | 25 | Yes | 2 | |
| 3 | 12 | Yes | 3 | |
| 4 | 6 | Yes | 4 | |
| 5 | 3 | Yes | 5 | |
| 6 | 1 | Yes | 6 | |
| End | 1 | No | 6 | 6 |
The algorithm counts how many times 100 can be halved (using integer division) before reaching 1. The answer is 6.
list = [5, 3, 8, 1]
FOR j = 0 TO 2
IF list[j] > list[j + 1] THEN
temp = list[j]
list[j] = list[j + 1]
list[j + 1] = temp
ENDIF
NEXT j
Trace Table:
| j | list[j] | list[j+1] | Swap? | temp | List After |
|---|---|---|---|---|---|
| 0 | 5 | 3 | Yes | 5 | [3, 5, 8, 1] |
| 1 | 5 | 8 | No | — | [3, 5, 8, 1] |
| 2 | 8 | 1 | Yes | 8 | [3, 5, 1, 8] |
After one pass: [3, 5, 1, 8]
Exam Tip: In GCSE exams, you will score marks for each correctly traced row. Even if you make an error partway through, you can still gain marks for subsequent rows if you follow through correctly from your (incorrect) values. This is called "error carried forward" (ECF). So always complete the entire trace table.
0 TO n-1 or 1 TO nTrace the following algorithm on list = [4, 1, 3, 2]:
list = [4, 1, 3, 2]
n = 4
FOR i = 0 TO n - 2
FOR j = 0 TO n - 2 - i
IF list[j] > list[j+1] THEN
temp = list[j]
list[j] = list[j+1]
list[j+1] = temp
ENDIF
NEXT j
NEXT i
OUTPUT list
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.