GCSE Computer Science

GCSE Computer Science trace tables: worked examples

Follow each change of value, choose the right branch and see exactly when a loop stops. Then try a trace-table practice question.

A reliable tracing method

An assignment stores a value in a variable. Selection chooses a branch; iteration repeats instructions. A condition evaluates to true or false. You need these meanings before filling the table.

  • Read the code and table headings

    Identify the variables, inputs, conditions and output. Follow any given starting row and instructions. Do not invent a value for a variable that has not been assigned.

  • Work out the right-hand side first

    For an update such as total ← total + n, use the current values to calculate the result, then replace total. Keep the old value in its earlier trace row.

  • Follow the actual path

    An IF executes its true branch or its ELSE branch, not both. A WHILE tests its condition before entering the body and tests again after each completed iteration.

  • Keep output separate from working

    A changing variable is not automatically displayed. Enter output only at OUTPUT, print or the equivalent instruction in the question.

  • Check the stopping point

    For a WHILE loop, evaluate the condition once more after the last update. A false result exits the loop; any statements after it still run.

Completed trace: the true branch

At x ← x + y, calculate 4 + 3, then store 7 in x. The condition is now 7 > 6, so it is true. Next, y ← x - y uses x = 7 and y = 3, giving 4. The ELSE assignment is skipped. The only output is 4.

Completed trace: the true branch

Values after each executed assignment, test or output. — means not yet assigned, no condition test or no output at this step. In the Output column it is a teaching placeholder, not displayed output.

Executed stepxyConditionOutput

x ← 4

4

—

—

—

y ← 3

4

3

—

—

x ← x + y

7

3

—

—

IF x > 6

7

3

True

—

y ← x - y

7

4

—

—

OUTPUT y

7

4

—

4

Completed trace: include the final condition

The body runs three times, but the condition is tested four times. On the last test, n is 4, so 4 <= 3 is false. The body does not run again. OUTPUT is outside the loop and displays 6 once. Final n is 4 even though the last value added was 3.

Completed trace: include the final condition

Values after each executed assignment, test or output. — means not yet assigned, no condition test or no output at this step. In the Output column it is a teaching placeholder, not displayed output.

Executed steptotalnConditionOutput

total ← 0

0

—

—

—

n ← 1

0

1

—

—

WHILE n <= 3

0

1

True

—

total ← total + n

1

1

—

—

n ← n + 1

1

2

—

—

WHILE n <= 3

1

2

True

—

total ← total + n

3

2

—

—

n ← n + 1

3

3

—

—

WHILE n <= 3

3

3

True

—

total ← total + n

6

3

—

—

n ← n + 1

6

4

—

—

WHILE n <= 3

6

4

False

—

OUTPUT total

6

4

—

6

Catch the mistakes that change the answer

Missing initialisation

Starting total at 1 instead of the stated 0 changes every later total. Record the starting assignments before tracing the loop.

Using an old value

In example 1, x has become 7 before the condition and subtraction. Using the original x = 4 would send you down the wrong branch.

Off-by-one errors

In example 2, changing n <= 3 to n < 3 would exclude n = 3: the loop would add only 1 and 2, and output 3. Read the comparison symbol exactly.

Stopping before the last update

The last successful iteration still executes n ← n + 1. Leaving final n as 3 misses that statement and the final false check.

Outputting on every row

Example 2 does not output 1, then 3, then 6. Those are working totals. OUTPUT is after ENDWHILE, so only the final 6 is displayed.

Practice answer and complete trace

There are three iterations, using n = 2, 3 and 4. They add 1, then 2, then 1 to score: 0 + 1 + 2 + 1 = 4. Final n is 5, final score is 4, and the only output is 4. The equality condition is true only when n is 3. On leaving the loop at n = 5, the IF is not reached again.

Practice answer and complete trace

Values after each executed assignment, test or output. — means not yet assigned, no condition test or no output at this step. In the Output column it is a teaching placeholder, not displayed output.

Executed stepnscoreConditionOutput

n ← 2

2

—

—

—

score ← 0

2

0

—

—

WHILE n < 5

2

0

True

—

IF n = 3

2

0

False

—

score ← score + 1

2

1

—

—

n ← n + 1

3

1

—

—

WHILE n < 5

3

1

True

—

IF n = 3

3

1

True

—

score ← score + 2

3

3

—

—

n ← n + 1

4

3

—

—

WHILE n < 5

4

3

True

—

IF n = 3

4

3

False

—

score ← score + 1

4

4

—

—

n ← n + 1

5

4

—

—

WHILE n < 5

5

4

False

—

OUTPUT score

5

4

—

4

AQA and OCR: read the notation you are given

The tracing method is the same, but symbols and loop conventions need careful reading. These notes use AQA’s 2024 pseudocode guide and the OCR J277 specification, version 3.1 (May 2026).

Assignment and equality

AQA uses ← to assign a value and = to test equality. OCR Exam Reference Language uses = for assignment and == for equality. Decide what the symbol means in that notation before changing a table value.

WHILE and FOR need different handling

Our WHILE examples update n explicitly and test again with that new value. Do not transfer that final out-of-range counter value to every FOR loop. AQA’s guide says its FOR counter never exceeds the stated bounds and ceases to exist after the loop. Follow the rules of the notation used in the question.

Use the supplied trace layout

AQA’s trace-table teaching guidance recognises more than one correct layout, including changes only or repeated unchanged values. Our tables repeat values to show the full state. Follow the question’s columns, starting entries and instructions rather than assuming every executed statement must always have a separate row.

Leave non-output cells empty

In OCR’s June 2024 J277/02 question 9(b), repeated unchanged variable values were allowed, but any entry in the output column counted as output, including a dash or zero. Leave non-output cells empty when completing that exam table. Our worked tables use a labelled em dash for readability; it is a teaching placeholder, not an entry to copy into an exam answer. The example illustrates why table instructions and question-specific mark schemes matter.

Support and clarity

Frequently asked questions

Straight answers to the questions people ask most often.

Does a blank cell mean zero?

No. A blank cell can mean that no new value is entered at that point, depending on the table convention. Zero is a specific value and should come from the algorithm. Our variable columns use — for not yet assigned and repeat unchanged values; our output columns use — to mean nothing is displayed. That is a teaching placeholder: follow the exam instructions and leave non-output cells empty where required.

What if a WHILE condition is false at the start?

Skip the loop body entirely and continue with the first instruction after ENDWHILE. A WHILE is a pre-condition loop: it may execute zero times. Do not assume every loop must run once.

Does a correct trace prove that a program is correct?

A trace shows the behaviour for the inputs and path you followed. It can reveal a logic error, but one successful trace does not establish that every possible input or branch works. Use other appropriate test cases as well.

Sources and references

Sources and references