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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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 is false, and the output is (which is ).
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 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 true.
Pass 1: a becomes 3, b becomes 5, output "3 5". Condition true.
Pass 2: a becomes 5, b becomes 4, output "5 4". Condition 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 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
- Follow and write algorithms (flowcharts, pseudocode, program code) that use sequence, selection, repetition (count-controlled, condition-controlled) and iteration with input, processing and output, and that use variables, constants and one- and two-dimensional data structures (strings, records, arrays).
A focused answer to Edexcel GCSE Computer Science 1.2.1 and 1.2.2, covering writing and following algorithms with sequence, selection and repetition, input-process-output, and variables, constants, strings, records and arrays.
- Follow and write algorithms that use arithmetic operators (addition, subtraction, division, multiplication, modulus, integer division, exponentiation), relational operators (equal to, less than, greater than, not equal to, less than or equal to, greater than or equal to) and logical operators (AND, OR, NOT).
A focused answer to Edexcel GCSE Computer Science 1.2.3, covering the arithmetic operators including modulus and integer division, the relational operators, and the logical operators AND, OR and NOT in algorithms.
- Understand syntax, logic and runtime errors and correct logic errors in algorithms; understand how the standard algorithms (bubble sort, merge sort, linear search, binary search) work; and use logical reasoning and test data to evaluate an algorithm's fitness for purpose and efficiency.
A focused answer to Edexcel GCSE Computer Science 1.2.5, 1.2.6 and 1.2.7, covering syntax, logic and runtime errors, the bubble sort, merge sort, linear search and binary search, and evaluating an algorithm's efficiency.
- Use decomposition and abstraction to solve problems; read, write, analyse and refine programs; convert algorithms (flowcharts, pseudocode) into programs; use techniques (layout, indentation, comments, meaningful identifiers, white space) for readable code; identify, locate and correct logic, syntax and runtime errors; and evaluate a program's fitness for purpose and efficiency.
A focused answer to Edexcel GCSE Computer Science 6.1, covering using decomposition and abstraction in programs, converting algorithms to code, writing readable code, identifying and correcting errors, and evaluating fitness for purpose.
- Identify the structural components of programs (constants, variables, initialisation, assignment, sequence, selection, repetition, iteration, data structures, subprograms, parameters, input/output) and write programs that use sequencing, selection, repetition (count-controlled, condition-controlled) and iteration with single entry and exit points.
A focused answer to Edexcel GCSE Computer Science 6.2, covering the structural components of programs and writing Python programs that use sequence, selection, count-controlled and condition-controlled repetition, and iteration.
Sources & how we know this
- Pearson Edexcel GCSE (9-1) Computer Science (1CP2) specification — Pearson (2020)