x

Revision notes for Edexcel GCSE Computer Science Tracing algorithm output with trace tables. Open the guide for explanations and worked examples. Written against the Edexcel GCSE Computer Science (1CP2) specification, so the content matches what's examinable rather than general Computer Science background.

Tracing algorithm output with trace tables

What you'll learn

  • How to follow an algorithm using a given set of data.
  • How variables change when assignment statements run.
  • How to build and use a trace table.
  • How to find the output or the value of a variable at a particular point.

Why tracing matters

In GCSE Computer Science, you are often given an algorithm and asked:

  • What is the output?
  • What value does a variable hold after a certain line?
  • How many times does a loop run?
  • Which branch of a selection statement is followed?

A trace table helps you answer these questions accurately by recording the values of variables as the algorithm runs.

Definition

Algorithm

An algorithm is a clear, step-by-step method for solving a problem. In an exam, it might be shown as a flowchart, informal pseudocode, or Python 3 code.

Key prerequisites

Variables

A variable is a named storage location for a value. The value can change while the algorithm runs.

Examples of variable names:

  • total
  • count
  • score
  • largest
Definition

Assignment

Assignment means storing a value in a variable. In algorithms, Edexcel often uses the left arrow, such as total ← 0, meaning “put 0 into total”.

The right-hand side is worked out first, then the result is stored in the variable on the left.

For example:

total ← total + 5

means:

  • take the current value of total
  • add 5
  • store the new result back in total
Common Mistake

Treating assignment like algebra

total ← total + 5 is not an equation to solve. It is an update instruction. The old value of total is used to calculate the new value.

Example

Updating a variable

Suppose this algorithm runs:

LineInstruction
1score ← 4
2score ← score + 3
3score ← score * 2
4OUTPUT score
  1. Line 1 stores 4 in score, so the current value is 4.
  2. Line 2 uses the current value: score + 3 means 4 + 3, so score becomes 7.
  3. Line 3 uses the updated value: score * 2 means 7 * 2, so score becomes 14.
  4. Line 4 outputs the current value of score, so the output is 14.

Inputs and given data

A question may give you a set of data. This means the values that are entered into the algorithm when it asks for input.

For example, if the algorithm says:

LineInstruction
1INPUT firstNumber
2INPUT secondNumber

and the question says the data is:

8, 12

then:

  • firstNumber gets 8
  • secondNumber gets 12

The input values are usually used in the order given.

Tip

Use the data in order

If an algorithm has several INPUT instructions, match them to the given data one by one. Do not skip values or reuse a value unless the algorithm specifically says to.

Sequence, selection and iteration

Algorithms normally use three basic control structures.

Sequence

Sequence means instructions run in order, one after another.

Example:

LineInstruction
1x ← 2
2x ← x + 1
3OUTPUT x

Line 1 runs, then line 2, then line 3.

Selection

Selection means the algorithm chooses between different paths using a condition.

A condition is a test that is either TRUE or FALSE, such as:

  • age >= 18
  • score < 50
  • password = "cat123"
Definition

Selection

Selection is when an algorithm uses a condition to decide which instruction or group of instructions to run, often using IF, THEN, ELSE and END IF.

Example

Following an IF statement

Given this algorithm and input data 11:

LineInstruction
1INPUT age
2IF age >= 13 THEN
3category ← "teen"
4ELSE
5category ← "child"
6END IF
7OUTPUT category
  1. Line 1 stores the input value 11 in age.
  2. Line 2 tests age >= 13. Since 11 is not greater than or equal to 13, the condition is FALSE.
  3. Because the condition is FALSE, line 3 is skipped and the ELSE path is followed.
  4. Line 5 stores "child" in category, so line 7 outputs child.

Iteration

Iteration means repetition. An algorithm repeats instructions using a loop.

Common loop types include:

  • FOR loops, which repeat a set number of times.
  • WHILE loops, which repeat while a condition is TRUE.
Definition

Iteration

Iteration is when an algorithm repeats one or more instructions. Each repeat of the loop is called an iteration.

Common Mistake

Stopping a WHILE loop too late

In a WHILE loop, check the condition before each repeat. As soon as the condition is FALSE, the loop body does not run again.

What a trace table is

A trace table is a table used to record the changing values of variables as an algorithm runs.

It usually includes columns for:

  • the line number or step
  • each important variable
  • any condition being tested
  • any output produced
Definition

Trace table

A trace table records the values of variables at different points while an algorithm is executed, so you can work out the final output or a variable’s value at a chosen point.

The diagram shows the main idea: follow the algorithm in order and record changes as they happen.

A simple algorithm being traced step by step into a trace table

Key Idea

Trace tables show state

The state of an algorithm means the current values of its variables. A trace table lets you track the state as each instruction changes it.

How to build a trace table

Step 1: Add useful columns

You usually need a column for each variable.

For example, if the algorithm uses:

  • count
  • total
  • number

then your trace table should probably include those three variables.

You may also add:

  • line to show which line has just run
  • condition to show whether a loop or IF test is TRUE or FALSE
  • output to record anything displayed by OUTPUT

Step 2: Work through the algorithm in order

Start at line 1 and follow the instructions exactly.

Each time a variable changes, record the new value.

If a condition is tested, record whether it is TRUE or FALSE if that helps you follow the path.

Step 3: Be careful with loops

For a loop:

  1. Check whether the loop should run.
  2. Trace all instructions inside the loop.
  3. Update any loop counter or variable.
  4. Return to the loop condition.
  5. Stop only when the loop condition is no longer satisfied.
Tip

Trace one line at a time

Cover the lines below the one you are tracing, or use your finger to follow the current line. This reduces the chance of jumping ahead and using a future value too early.

Worked trace table with a loop

Consider this algorithm:

LineInstruction
1total ← 0
2count ← 1
3WHILE count <= 4
4total ← total + count
5count ← count + 1
6END WHILE
7OUTPUT total

The question might ask:

  • What value does total hold when line 7 is reached?
  • What is the output?
Example

Tracing a WHILE loop

  1. Lines 1 and 2 initialise the variables: total becomes 0 and count becomes 1.
  2. At line 3, count <= 4 is tested. Since count is 1, the condition is TRUE, so the loop body runs.
  3. Line 4 adds the current count to total: 0 + 1 gives total = 1.
  4. Line 5 increases count by 1, so count becomes 2.
  5. The algorithm returns to line 3 and repeats while count is 2, 3 and 4, adding each value to total.
  6. When count becomes 5, the test count <= 4 is FALSE, so the loop stops and line 7 outputs the final total.

A completed trace table could look like this:

LinecounttotalCondition count <= 4Output
10
210
310TRUE
411
521
321TRUE
423
533
333TRUE
436
546
346TRUE
4410
5510
3510FALSE
751010

So when line 7 is reached:

  • total holds 10
  • the output is 10

Finding a variable at a given point

Sometimes the question does not ask for the final output. It may ask something like:

What value does count hold after line 5 has executed for the third time?

This is exactly what trace tables are for. You trace carefully until the required point, then read the value from the correct column.

Key Idea

Point in the algorithm matters

A variable can have different values before and after the same line runs. Always check whether the question asks for the value before, after, or when reaching a line.

For the loop above:

  • before line 5 runs for the first time, count is 1
  • after line 5 runs for the first time, count is 2
  • after line 5 runs for the third time, count is 4
Common Mistake

No output if OUTPUT is not reached

An algorithm only produces output when an OUTPUT instruction is actually executed. If a loop never ends, or a branch skips the OUTPUT, there may be no output from that part of the algorithm.

Recording output correctly

Output is not always the same as the final value of every variable.

An algorithm may calculate several variables but only output one of them.

For example:

LineInstruction
1a ← 5
2b ← 9
3a ← a + b
4OUTPUT b

At the end:

  • a is 14
  • b is 9
  • the output is 9

The output is 9 because line 4 outputs b, not a.

Common Mistake

Outputting the wrong variable

Do not assume the algorithm outputs the most recently changed variable. Always check the exact variable named in the OUTPUT instruction.

A compact method for exams

When you are short on time, you do not always need a beautiful table. You need an accurate one.

A quick trace table might include only the variables that change and the output.

For example:

StepitotalOutput
start0
111
223
336
end366

This is fine if it clearly shows the values you need.

Exam technique

In the exam

  1. Write down the given input data first, then use the values in order as each INPUT instruction is reached.
  2. Trace one instruction at a time, updating variables only when an assignment statement actually runs.
  3. For IF and WHILE, evaluate the condition using the current variable values, then follow only the correct path.
  4. When asked for output, copy exactly what the OUTPUT instruction would display, not just the final value of a random variable.
Self review

Check yourself

  • In total ← total + number, which value of total is used first: the old value or the new value?
  • Why is it useful to include a condition column when tracing a WHILE loop?
  • How could the final output be different from the final value of a variable?
You've reached the end

Test yourself on this topic, or move on to the next guide.

FlashcardsSelf-test with active recall
Types of error and correcting logic errorsUp next

How was this guide?

Tracing algorithm output with trace tables Revision Guide

  1. GCSE
  2. /Computer Science
  3. /Tracing algorithm output with trace tables