Session XP: 0
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
LineixOutput
x = 00
i = 110
x = x + i11
i = 221
x = x + i23
i = 333
x = x + i36
i = 446
x = x + i410
PRINT x41010
Example 2 — with conditional
a = 10
b = 3
WHILE a > b
    a = a - b
END WHILE
PRINT a
Trace table for Example 2
aba > b?Output
103
103True
73True
43True
13False — exit loop
131

Common exam traps

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?
+40 XP

Module complete! 🎉

Score loading...

+10 XP