RecallStrideA BreakellSystems productTry revision

How to complete a pseudocode trace table

Record what a program actually does, one executed statement at a time, with an original worked example.

A trace table records the changing state of a program as it executes. It helps you predict output, explain a loop and locate a logic error. The key is to follow the written instructions in order, including every condition check, rather than writing the answer you think the programmer intended.

Start with the variables and the input

Read the entire algorithm first. Identify the variables, initial assignments, loop condition and output statements. If there is input, write down the supplied values in the order they will be consumed. Do not invent missing input. Leave unassigned variables blank until the program assigns them.

Trace this program. In the RecallStride interpreter, TO includes the end value.

total = 0
for count = 1 to 4
  total = total + count
next count
print(total)

Work through the assignments

The initial assignment sets total to zero. The first iteration sets count to 1 and evaluates the right-hand side using the existing total: 0 + 1. Only then is 1 stored in total. The next iteration adds 2 to that new value. Assignment changes the stored value; it is not a mathematical claim that both sides were always equal.

Trace after initialisation and each completed loop body
StagecounttotalOutput
Initial assignment—0—
First body11—
Second body23—
Third body36—
Fourth body410—
After loop—1010

This table deliberately records completed loop bodies; it does not make a claim about the final value left in the loop variable by every pseudocode dialect. If an exam supplies a particular table or convention, follow it. If individual assignment statements must occupy separate rows, split each iteration into the required rows.

Distinguish state from output

Changing total does not display it. The example has one print statement after the loop, so it displays only 10. Move print inside the loop and the program displays 1, 3, 6 and 10 on successive lines. A common error is to put every intermediate variable value in the output column even when no output instruction ran.

Trace a condition at the right time

A while loop tests before executing its body, so it may run zero times. A repeat-until loop tests after the body and runs at least once. With nested loops, complete the inner loop for each outer iteration. Record array indices separately from the values stored at those indices; an index of 2 does not mean the stored value is 2.

Find off-by-one errors

Mark whether each bound is inclusive. For a zero-indexed array of length four, the valid indices are 0, 1, 2 and 3. An algorithm that accesses index 4 has gone beyond the array. For a counter-controlled loop, list the exact counter sequence before counting the iterations. A loop from 1 to 4 inclusive has four iterations; a loop continuing while count < 4 from an initial 1 has three.

Practice: what if total starts at 5?

The completed-body totals become 6, 8, 11 and 15. The final output is 15.

Practice: what if print(total) is inside the original loop?

There are four output lines: 1, 3, 6, 10. Each line records total after that iteration's addition.

Predict the result first, then run it in the free pseudocode interpreter. If your prediction differs, compare the first point of disagreement in the variable trace. This is more useful than replacing your whole table with the final answer.

Related course topics

OCR J277 2.1.2 · OCR H446 2.2.1

Scope and sources

These original RecallStride explanations and examples are for study. Consult the OCR GCSE J277 specification (PDF) and OCR A-Level H446 specification (PDF) for qualification requirements. Not every detail applies at the same depth to both courses. Read the editorial policy or report an error to support@recallstride.com.

Continue learning