GCSE Computer Science · public explanation

Representing and Tracing Algorithms

Follow an algorithm step by step using trace tables and representations. Track variables, conditions and output, including absent targets and empty inputs.

Read the explanation and worked examples, then choose your exam board to continue with activities and practice in your account.

OCR J277On the specificationOCR J277 2.1.2

Apply representing and tracing algorithms using OCR Exam Reference Language or a studied language only when requested.

AQA 8525 (2027+)On the specificationAQA 8525 (2027+) 3.1.1

Apply representing and tracing algorithms in the precise response format printed on the paper.

Track values, not just the final answer

Consider this invented checkpoint task: find the first position of code 9 in [14, 5, 9, 3, 12], counting positions from zero, or report Absent. The required position is 2, but knowing that answer does not show how the method reaches it. A representation makes the instructions or their execution visible.

A variable is a named place holding a value that may change. The state is the current set of variable values. Here index selects a position, current holds a code read from that position, and found records True or False. The assignment index ← index + 1 means read the old index, add one and store the result back in index. It is not a mathematical claim that a number equals itself plus one.

A flowchart shows instructions and possible routes with shapes and arrows. Pseudocode gives precise textual instructions without requiring a runnable program. A trace table records the states and outputs actually reached for supplied inputs. The first two describe a method; the trace is evidence from following it. Follow the notation supplied by the question when tracing a printed program.

VariableBefore instructionInstruction, in mathematical notationAfter instruction
index0index ← index + 11
current14No new assignment to current14

State the method before tracing it

For the worked lookup, initialise index to 0 and found to False. Continue while index is less than the number of codes and found is False. On each repetition, read the code at index into current. If current matches the target, set found to True; otherwise increase index by one.

After the loop, display index if found is True; otherwise display Absent. Check the loop condition before accessing the array. This keeps an absent target or an empty input from causing a read beyond the final valid position.

Record the state at the specified point

A trace row does not always mean one loop iteration. Some questions ask for changes after individual executed instructions; others print a row for the end of each complete loop body. Read that instruction before entering values. In this worked table each body row is a snapshot AFTER the selection and any index update. Unchanged values are repeated because these are complete snapshots.

In body 1, current becomes 14, the comparison with 9 is False, and index becomes 1. The row therefore has index 1 but current 14. Body 2 reads 5 and finishes with index 2. Body 3 reads 9, sets found to True and leaves index at 2. The guard then becomes False because NOT found is False, so there is no fourth body.

No output occurs during these three bodies. The dash in Output means no output event, not a displayed dash or zero. Only after the loop does the method display 2. If the question instead asks for changed values after every assignment, give separate rows for reading current and advancing index; do not merge those events into this snapshot format.

ObservationindexcurrentfoundOutput
Initialised0Not assignedFalse—
After body 1114False—
After body 225False—
After body 329True—
Output29True2

Trace a search that never finds its target

Return to the lookup with a new target. For an independent request 8, none of [14, 5, 9, 3, 12] matches. The five complete bodies finish with index values 1, 2, 3, 4 and 5, while found remains False. At index 5 the remaining-position test is False. The loop ends without reading codes[5].

The output selection then follows its ELSE branch and displays Absent. The last value held in current is 12, because that was the last item read. A stored current value is not automatically an output. Every independent trace restarts the initial state; the True flag from the earlier request-9 run must not leak into this run.

For another separate run, supply an empty array and count 0. index begins at 0, so 0 < 0 is False at the first guard. No body runs, current is never assigned and Absent is output. Do not invent current = 0 or an extra array read simply to fill a table cell; use the convention specified for unassigned values.

ObservationindexcurrentfoundOutput
Initialised0Not assignedFalse—
After body 1114False—
After body 225False—
After body 339False—
After body 443False—
After body 5512False—
Output512FalseAbsent

Use the trace to explain purpose and locate errors

A purpose is what the method achieves, not a list of line numbers. The lookup takes an ordered list and requested code, compares items from the start and returns the first matching position or Absent. A different method could instead add all five codes and output 43. Same input data does not imply the same algorithm or purpose.

A logic error can use valid-looking instructions but produce the wrong behaviour. Remove the lookup's index increment: after reading 14 for request 9, the next body reads 14 again and no progress is made. In contrast, a malformed language instruction is a syntax error. The board-specific equality and assignment symbols matter when interpreting or completing the supplied code.

Show where the result first diverges, propose a general repair and test it on a fresh case. Include the last valid index and an empty input in your checks. For OCR, keep a structure diagram's task hierarchy separate from a flowchart's chronological execution routes.

Continue with your exam board

The course combines lesson videos, interactive activities, topic questions and original exam mocks. Sign in to use the learning tools and save your progress; your account’s course access applies.

Independent learning resources. Not endorsed by AQA or OCR. These public introductions complement the full course and the official specification.