You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Trace tables are used to track how variables change as an algorithm executes. Completing trace tables is a common exam skill in OCR J277 Paper 2, typically worth 3-6 marks. This lesson teaches you how to trace through code systematically and avoid common errors.
A trace table is a table with a column for each variable and each output. Each row represents one step of the algorithm (usually one line of code or one iteration of a loop). By filling in the table, you can see exactly how the algorithm behaves.
OCR Exam Tip: Cross out each line of code as you trace through it (on the question paper) to keep track of where you are. For loops, use a tally to count iterations.
The trace-table construction flow — repeated for each line of code — is shown below:
flowchart TD
A[Identify all variables] --> B[Add Output column]
B --> C[Record initial values]
C --> D[Read next line of code]
D --> E{What kind of statement?}
E -->|Assignment| F[Update changed variable only]
E -->|Loop iteration| G[Add new row, update loop var]
E -->|Print| H[Record value in Output column]
E -->|If/else| I[Evaluate condition, follow branch]
F --> J{More code lines?}
G --> J
H --> J
I --> J
J -->|Yes| D
J -->|No| K[Final Output is the answer]
x = 1
total = 0
for i = 1 to 4
total = total + x
x = x * 2
next i
print(total)
| Step | i | x | total | Output |
|---|---|---|---|---|
| Init | — | 1 | 0 | |
| i=1 | 1 | 1 | 1 | |
| 1 | 2 | 1 | ||
| i=2 | 2 | 2 | 3 | |
| 2 | 4 | 3 | ||
| i=3 | 3 | 4 | 7 | |
| 3 | 8 | 7 | ||
| i=4 | 4 | 8 | 15 | |
| 4 | 16 | 15 | ||
| End | 15 |
The output is 15.
count = 0
total = 0
num = input("Enter a number (0 to stop): ")
while num != 0
total = total + num
count = count + 1
num = input("Enter a number (0 to stop): ")
endwhile
print(total / count)
If the user enters: 10, 20, 30, 0
| Step | num | count | total | Output |
|---|---|---|---|---|
| Init | — | 0 | 0 | |
| Input | 10 | 0 | 0 | |
| Loop 1 | 10 | 1 | 10 | |
| Input | 20 | 1 | 10 | |
| Loop 2 | 20 | 2 | 30 | |
| Input | 30 | 2 | 30 | |
| Loop 3 | 30 | 3 | 60 | |
| Input | 0 | 3 | 60 | |
| End | 20 |
The output is 20 (the average of 10, 20, and 30).
age = 17
hasTicked = true
if age >= 18 then
if hasTicked == true then
print("Entry allowed")
else
print("Must agree to terms")
endif
else
print("Too young")
endif
Trace:
| age | hasTicked | Output |
|---|---|---|
| 17 | true | Too young |
scores = [5, 8, 3, 9, 2]
highest = scores[0]
for i = 1 to 4
if scores[i] > highest then
highest = scores[i]
endif
next i
print(highest)
| Step | i | scores[i] | highest | Output |
|---|---|---|---|---|
| Init | — | — | 5 | |
| i=1 | 1 | 8 | 8 | |
| i=2 | 2 | 3 | 8 | |
| i=3 | 3 | 9 | 9 | |
| i=4 | 4 | 2 | 9 | |
| End | 9 |
The output is 9 (the highest value in the array).
| Mistake | Correction |
|---|---|
| Updating variables that have not changed | Only write a new value when the code explicitly changes it |
| Getting loop boundaries wrong | Check whether the loop is inclusive (1 to 5 means 1, 2, 3, 4, 5) |
| Forgetting the initial values | Always fill in the starting values before the loop begins |
| Confusing = (assignment) with == (comparison) | x = 5 assigns the value 5 to x; x == 5 checks if x equals 5 |
| Incorrect array indexing | Arrays start at index 0 in OCR pseudocode |
| Not recording outputs | Include a separate Output column and record every print statement |
OCR Exam Tip: If the exam provides a partially completed trace table, fill in only the blank cells. Do not alter cells that are already filled in — they are correct and serve as a guide.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.