1CP2 P1Edexcel GCSE Computing · 1CP2 Paper 1 (Programming)
Trace Tables and Dry Running Code
How to trace code line by line, complete trace tables, predict output and spot bugs.
—
Best score
—
Last attempt
Best attempt progress
0
XP earned
Watch first
Craig n DaveTrace tables and dry running
Craig n DaveLoops and iteration in pseudocode
Reference & Examples
What is a trace table?
A trace table records the value of each variable after each line of code executes.
Used to "dry run" code — follow it line by line without running it on a computer.
Shows how variables change during loops and conditionals.
In exams: given a piece of code and asked to complete the trace table.
Common exam pattern: spot what the output is, or find a bug by spotting wrong values.
How to complete a trace table
1. Read the code, identify all variables and any output.
2. Create columns: one per variable, one for output.
3. Go through the code line by line. When a variable changes, enter the NEW value.
4. When output occurs, note it in the output column.
5. Leave a cell blank if the variable does not change on that line.
6. Be especially careful with loops: track the loop counter AND the loop body.
Example 1 — simple loop
x = 0
FOR i = 1 TO 4
x = x + i
END FOR
PRINT x
Trace table for Example 1
Line
i
x
Output
x = 0
–
0
i = 1
1
0
x = x + i
1
1
i = 2
2
1
x = x + i
2
3
i = 3
3
3
x = x + i
3
6
i = 4
4
6
x = x + i
4
10
PRINT x
4
10
10
Example 2 — with conditional
a = 10
b = 3
WHILE a > b
a = a - b
END WHILE
PRINT a
Trace table for Example 2
a
b
a > b?
Output
10
3
10
3
True
7
3
True
4
3
True
1
3
False — exit loop
1
3
1
Common exam traps
Forgetting 0-indexed arrays: scores[0] is the first element.
Off-by-one errors: FOR i = 1 TO 5 runs 5 times (1,2,3,4,5). FOR i = 0 TO 4 also runs 5 times.
WHILE loop: check condition BEFORE entering. If condition is False at start, body never runs.
Nested loops: complete the inner loop fully before incrementing the outer loop counter.
Modifying a variable used in the loop condition: can create infinite loops.
Exam practice — 5 questions · 12 marks · 1CP2 Paper 1 style
⚡ Write your pseudocode or answer — the AI marks against the Edexcel 1CP2 Paper 1 mark scheme.
4 marksTrace a loop1CP2 Paper 1
Trace this code and complete the trace table. State the final output.
count = 0
total = 0
FOR i = 1 TO 5
IF i MOD 2 == 0 THEN
total = total + i
count = count + 1
END IF
END FOR
PRINT total
PRINT count
Hint: Only execute the IF body when i MOD 2 == 0 (i.e. when i is even: 2 and 4). Track total and count separately.
+40 XP
1 markWHILE condition1CP2 Paper 1
In a WHILE loop, when is the condition checked?
A Only once, before the loop starts
B Before every iteration, including the first
C After every iteration
D Only when a variable changes
4 marksPredict output1CP2 Paper 1
What is the output of this code? Show your working as a trace.
x = 1
WHILE x < 20
x = x * 2
END WHILE
PRINT x
Hint: Keep doubling x until x >= 20. Note: the loop runs while x < 20, so it exits when x first reaches or exceeds 20.
+40 XP
1 markFOR loop count1CP2 Paper 1
How many times does the body of this loop execute?
FOR i = 3 TO 7
PRINT i
END FOR
A 4
B 5
C 7
D 3
4 marksFind the bug1CP2 Paper 1
This code is supposed to count from 10 down to 1 and print each number. Identify and fix the bug.
i = 10
WHILE i > 0
PRINT i
END WHILE
Hint: Ask: does anything inside the loop change the condition? If not, the condition stays True forever. What line is missing?