Skip to main content
EnglandComputer ScienceSyllabus dot point

How do you use a trace table to work out exactly what an algorithm does, step by step?

Determine the correct output of an algorithm for a given set of data, and use a trace table to determine what value a variable will hold at a given point in an algorithm.

A focused answer to Edexcel GCSE Computer Science 1.2.4, covering how to dry-run an algorithm, build a trace table row by row, and determine the value a variable holds and the output for given input data.

Generated by Claude Opus 4.89 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. What a trace table is
  3. How to build a trace table
  4. A worked trace
  5. Determining a value at a given point
  6. Try this

What this dot point is asking

Edexcel wants you to work out exactly what an algorithm does for given input, and to use a trace table to record the value of each variable as the algorithm runs, so you can state what a variable holds at any point and what is output.

What a trace table is

Tracing is how you answer "what is the output" and "what value does this variable hold" questions reliably, and it is also how you find logic errors, because the table shows the exact point where a value goes wrong. The skill is being methodical: change only one thing per step, and never skip ahead in your head.

How to build a trace table

The two most common mistakes are running a loop one pass too many (always re-check the condition before assuming another pass happens) and forgetting to record the output column. Keep the output column separate from the variables so you do not confuse "what is stored" with "what is printed".

A worked trace

Consider this algorithm, which counts down and accumulates a total:

total = 0
n = 4
while n > 0
    total = total + n
    n = n - 1
end while
print(total)

We check n > 0 before each pass. The trace table records total and n and any output:

Step n total Output
Initialise 4 0
Pass 1 (n is 4) 3 4
Pass 2 (n is 3) 2 7
Pass 3 (n is 2) 1 9
Pass 4 (n is 1) 0 10
Condition n > 0 now false, exit
print(total) 0 10 10

The loop stops when n reaches 0 because 0>00 > 0 is false, and the output is 1010 (which is 4+3+2+14 + 3 + 2 + 1).

Determining a value at a given point

A common Edexcel variant asks for the value a variable holds at a specific point, not just at the end, for example "what is the value of total immediately after the third pass of the loop". The method is the same: trace down to that point and read off the row. This is exactly why each row records the state after one step, so you can answer about any moment in the run, not only the final result.

Try this

Q1. State what a trace table is used for. [1 mark]

  • Cue. To dry-run an algorithm by hand and track how variable values change (to find the output or a value at a point, or to locate a logic error).

Q2. For x = 10, then while x > 6: x = x - 2 end while, state the final value of x. [1 mark]

  • Cue. 6. Passes give 8 then 6; at 6 the condition 6>66 > 6 is false, so the loop stops.

Exam-style practice questions

Practice questions written in the style of Pearson Edexcel exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Edexcel 20224 marksComplete a trace table to show the values of a, b and the output for the following algorithm. a = 1, b = 6, then WHILE a < b: a = a + 2, b = b - 1, print(a, b), end while.
Show worked answer →

Trace the loop one full pass at a time, only writing a value when it changes, and check the condition a < b before each pass.

Start: a = 1, b = 6. Condition 1<61 < 6 true.
Pass 1: a becomes 3, b becomes 5, output "3 5". Condition 3<53 < 5 true.
Pass 2: a becomes 5, b becomes 4, output "5 4". Condition 5<45 < 4 false, so the loop stops.

Final values a = 5, b = 4, total output "3 5" then "5 4".

Markers reward a row per pass with correct updated values and the matching output, and stopping the loop at the right point (when a<ba < b first becomes false). A common error is doing one pass too many.

Edexcel 20213 marksUsing the algorithm below, state the value of total after it has finished. total = 0, FOR i = 1 TO 5: IF i MOD 2 = 0 THEN total = total + i END IF, NEXT i.
Show worked answer →

Trace each value of i, adding to total only when i MOD 2 = 0 (i is even).

i = 1: odd, no change (total 0). i = 2: even, total becomes 2. i = 3: odd, no change. i = 4: even, total becomes 6. i = 5: odd, no change.

So the final value of total is 6 (the sum of the even numbers 2 and 4).

Markers reward identifying that only even i values are added, the correct running total, and the final answer 6. The discriminator is correctly skipping the odd values.

Related dot points

Sources & how we know this