Spec & Goals 3 min
AQA Spec 3.1.1 · Interpreting and completing algorithms
By the end of this lesson you can:
- Explain what a trace table is and why we use one.
- Trace an algorithm line by line, recording every variable and the output.
- Complete a trace table for a loop and state the final output.
Warm-Up 5 min
Last lesson you wrote algorithms in pseudo-code and flowcharts. Now we check that an algorithm actually works.
Quick starter
Read these three lines and work out the value of x at the end:
x ← 4 x ← x + 3 x ← x * 2
Reveal the answer
x becomes 4, then 7, then 14. Each line changes the value — so we follow them in order. A trace table writes down every change for us.
Key Concept — what a trace table is 14 min
A trace table records the value of every variable after each step of an algorithm. You run the algorithm by hand, writing one row each time a line changes something.
Why we use one
- To check an algorithm works — does it give the right answer?
- To find bugs — the table shows exactly where a value goes wrong.
How to build one
Set up the columns first, then fill the rows:
- One column per variable, plus an Output column.
- One row per executed line that changes a value or produces output.
- Each row carries forward the values that did not change.
An empty trace table for a program with one variable total and an output looks like this:
| Line | total | Output |
|---|---|---|
| 1 | ||
| 2 |
Worked Example — tracing a FOR loop 12 min
Problem: trace the algorithm below and state what it outputs.
AQA pseudo-code
total ← 0
FOR i ← 1 TO 3
total ← total + i
ENDFOR
OUTPUT totalWe have two variables, total and i, plus an Output column. We build the table row by row.
total ← 0setstotalto 0.- The loop runs with
i= 1, then 2, then 3. Each pass addsitototal. - After the loop,
OUTPUT totaldisplays the final value.
| Line / step | i | total | Output |
|---|---|---|---|
total ← 0 | 0 | ||
loop pass 1 (i = 1) | 1 | 1 | |
loop pass 2 (i = 2) | 2 | 3 | |
loop pass 3 (i = 3) | 3 | 6 | |
OUTPUT total | 3 | 6 | 6 |
The algorithm outputs 6 — it adds up 1 + 2 + 3.
The same algorithm in Python
total = 0 for i in range(1, 4): total = total + i print(total)
Try It Yourself 12 min
Goal: Complete a trace table for this countdown and state the output.
count ← 3
WHILE count > 0
OUTPUT count
count ← count − 1
ENDWHILEHint: write a row each time the loop body runs — the loop stops when count reaches 0.
Goal: Trace this doubling loop. List every value of num and the final output.
num ← 1
FOR i ← 1 TO 4
num ← num * 2
ENDFOR
OUTPUT numHint: the loop runs four times — so your table needs four loop rows.
Goal: Trace this algorithm and explain in one sentence what it works out.
product ← 1
FOR i ← 1 TO 4
product ← product * i
ENDFOR
OUTPUT productHint: follow product carefully — multiplying 1 × 2 × 3 × 4.
📝 Exam Practice 10 min
Answer the way the examiner expects — the command word and the marks tell you how much to write.
Define the term trace table.
Mark scheme
- A table that records the value of each variable (line by line) as an algorithm runs (1).
Complete the trace table for the algorithm below.
total ← 10
FOR i ← 1 TO 3
total ← total − i
ENDFOR
OUTPUT total| Step | i | total | Output |
|---|---|---|---|
total ← 10 | |||
| loop pass 1 | |||
| loop pass 2 | |||
| loop pass 3 | |||
OUTPUT total |
Mark scheme
- After pass 1:
i= 1,total= 9 (1). - After pass 2:
i= 2,total= 7 (1). - After pass 3:
i= 3,total= 4; Output column shows4(1).
State the final output of the algorithm in the previous question.
Mark scheme
4(1).
Give one reason a programmer might use a trace table.
Mark scheme
- To check the algorithm works / produces the correct result or to find (locate) a bug / error (1).
Recap & Key Terms 3 min
A trace table records every variable after each step of an algorithm. You write one column per variable plus Output, and one row per executed line. We use it to check an algorithm works and to find bugs.
- Trace table
- A table that records the value of each variable, line by line, as an algorithm runs.
- Variable
- A named store that holds a value which can change while the algorithm runs.
- Iteration
- Repeating a set of steps — a loop, which produces several rows in the trace table.
- Output
- A value the algorithm displays, recorded in the Output column of the trace table.
Homework 1 min
Task (≤ 15 min): Complete a trace table for the algorithm below, then state the final output.
total ← 0
FOR i ← 1 TO 5
total ← total + i
ENDFOR
OUTPUT totalModel answer
| Step | i | total | Output |
|---|---|---|---|
total ← 0 | 0 | ||
| pass 1 | 1 | 1 | |
| pass 2 | 2 | 3 | |
| pass 3 | 3 | 6 | |
| pass 4 | 4 | 10 | |
| pass 5 | 5 | 15 | |
OUTPUT total | 5 | 15 | 15 |
Award marks for: correct total after each pass (1), and the final output 15 (1).