Home / GCSE · Edexcel / Computer Science / Computational thinking

CPX3Trace Tables and Errors

Edexcel 1CP2 · 1.2.4

Working out the output of an algorithm

To find the output for given input, follow the algorithm one line at a time, exactly as the computer would.

  • Keep track of each variable’s value as it changes. Write values down: don’t hold them in your head.
  • For if … elif … else, the conditions are checked from the top. The first one that is True runs, and the rest are skipped.
  • Pay close attention to boundaries: > and >= give different results when the value is exactly on the boundary.
  • Give exactly what is printed, in order. Commas in print put a space between the items.
Worked exampleState the output for inputs of 25, 12 and 11.
1  temperature = int (input ("Temperature: "))
2  if (temperature >= 25):
3      advice = "Sun cream"
4  elif (temperature >= 12):
5      advice = "Light jacket"
6  else:
7      advice = "Coat"
8  print (advice)
  1. 25: line 2, 25 >= 25 is Truethe first branch runs; the others are skipped
  2. 12: line 2 is False; line 4, 12 >= 12 is Truethe elif branch runs
  3. 11: lines 2 and 4 are both Falseso the else branch runs
Sun cream, Light jacket, Coat.
Exam tipTest the boundary values in your head first. Most wrong answers on output questions come from treating >= as >.
Edexcel 1CP2 · 1.2.4

What a trace table is for

A trace table records the value of each variable as you step through an algorithm line by line.

  • It shows the value a variable holds at any point in the algorithm.
  • It helps you work out the output for a given set of data.
  • It helps you work out the purpose of an algorithm you haven’t seen before: look at how the values change.
  • It helps you find logic errors: the row where a value first goes wrong shows where the mistake is.
  • A trace table is not a truth table. A truth table lists the outputs of a logical expression for every combination of inputs.
Exam tipIn 2023 many students described a trace table as “checking the output”. Say what it records: the values of the variables at each step.
Trace table
A table that records the value of each variable while stepping through an algorithm line by line.
Dry run
Working through an algorithm by hand, without a computer, usually with a trace table.
Edexcel 1CP2 · 1.2.4

Setting out a trace table

Exam trace tables give you the columns. Fill them in the same way every time.

  • One column for each variable, in the order given, plus an Output column.
  • The first row holds the starting values set before any loop. Students often miss it.
  • Start a new row each time round a loop (or each time a value changes). A blank cell means the value hasn’t changed.
  • Write in the Output column only on the row where a print runs. Many students write the output on every row and lose marks.
  • “You may not need to use all the rows”: Pearson gives spare rows on purpose. Don’t invent passes to fill them.

Each part of a trace table

Exam tipPearson usually gives one mark per correct row or group of rows, with follow-through. A slip early on doesn’t lose every later mark, so keep going carefully.
Edexcel 1CP2 · 1.2.4

Tracing selection inside a loop

When an if sits inside a loop, check the condition again on every pass.

Worked exampleComplete a trace table for this algorithm.
1  total = 0
2  for value in [4, 9, 2, 7]:
3      if (value > 5):
4          total = total + value
5      else:
6          total = total - 1
7  print (total)
  1. Row 1: total = 0the starting value
  2. value 4: 4 > 5 is False → total = −1else branch
  3. value 9: True → total = −1 + 9 = 8if branch
  4. value 2: False → total = 7else branch
  5. value 7: True → total = 14, then printthe print is after the loop
valuetotalOutput
0
4−1
98
27
714
14
Watch outLook at the indentation. A print indented inside the loop runs every pass; one at the left margin runs once, after the loop ends.
Edexcel 1CP2 · 1.2.4

Tracing count-controlled loops

for x in range (start, stop, step) starts at start, goes up in steps, and stops before it reaches stop.

CodeValues of the loop variable
range (0, 4)0, 1, 2, 3
range (1, 6)1, 2, 3, 4, 5
range (3, 12, 4)3, 7, 11
range (10, 0, -3)10, 7, 4, 1
Worked exampleTrace this loop.
1  total = 0
2  for number in range (3, 12, 4):
3      total = total + number
4      print (number, total)
  1. number = 3 → total = 3, output 3 3start value
  2. number = 7 → total = 10, output 7 103 + 4
  3. number = 11 → total = 21, output 11 21the next value, 15, is past the stop value 12
Three passes. The final total is 21.
Exam tipThe stop value is never used. In 2025 many students traced range (1, NUM + 1) with NUM = 6 as running to 7, one pass too many.
Edexcel 1CP2 · 1.2.4

Tracing condition-controlled and nested loops

A while loop checks its condition before each pass. When the condition is False, the loop ends straight away.

  • If the condition is False to begin with, a while loop runs zero times.
  • In a nested loop, the inner loop runs all the way through for each pass of the outer loop. A 2 × 3 list means 6 passes of the inner loop body.
Worked exampleTrace this algorithm.
1  bacteria = 3
2  minutes = 0
3  while (bacteria < 100):
4      bacteria = bacteria * 2
5      minutes = minutes + 20
6  print (minutes, bacteria)
  1. 3 → 6 → 12 → 24 → 48 → 96each time 96 or less is below 100, so the loop continues
  2. 96 < 100 is True → 192, minutes = 120one more pass
  3. 192 < 100 is Falsethe loop stops
Six passes. Output: 120 192.
Worked exampleState the output of this nested loop.
1  grid = [[2, 5, 1],
2          [4, 0, 3]]
3  for row in grid:
4      rowTotal = 0
5      for value in row:
6          rowTotal = rowTotal + value
7      print (rowTotal)
  1. row [2, 5, 1]: rowTotal resets to 0, then 2, 7, 8the inner loop runs 3 times
  2. print 8after the inner loop, still inside the outer loop
  3. row [4, 0, 3]: 0, then 4, 4, 7; print 7rowTotal is reset for each row
8 then 7.
Edexcel 1CP2 · 1.2.5

Syntax, runtime and logic errors

There are three types of error. Learn a definition for each, not just an example.

  • Syntax errors are found by the translator (compiler or interpreter) before the code can run.
  • Runtime errors depend on the data: the same program may work with one input and crash with another.
  • Logic errors produce no error message, so they are found by testing with data and by tracing.
TypeWhat it isExample
Syntax errorThe code breaks the rules (grammar) of the programming language, so it can’t be translatedif (score > 10) with the colon missing
Runtime errorThe program crashes while it is running because the computer is asked to do something impossibledividing by zero; int ("£4"); days[3] in a 3-item list
Logic errorThe program runs but gives the wrong result, because the algorithm is wrong> where >= was needed; a total reset inside a loop

When each type of error shows itself

Exam tip“Missing colon” is an example, not a definition. To define a syntax error, say it breaks the rules or grammar of the language.
Exam tipA runtime error is not “taking too long to run”. Explain it as the program crashing or stopping during execution, then give an example such as dividing by zero.
Syntax error
An error where the code breaks the rules (grammar) of the programming language.
Runtime error
An error that happens while the program is running, causing it to crash, e.g. dividing by zero.
Logic error
An error in the algorithm that lets the program run but gives an incorrect result.
Edexcel 1CP2 · 1.2.5

Finding logic errors with test data

Because a logic error gives no message, you find it by comparing the expected result with the actual result.

  • Choose test data where you already know the right answer.
  • Test values on a boundary (exactly 50 for “50 or more”) as well as either side.
  • Trace the code with the data. The first row where a value differs from what you expect points to the faulty line.
  • Common logic errors: the wrong relational operator (> v >=); a range that stops one too early or starts at the wrong index; a total or count reset inside a loop; the wrong variable used; a line indented into (or out of) a loop by mistake; a calculation in the wrong order (missing brackets).
Worked exampleThis should print every multiple of 5 from 5 to 50. Find the logic error.
1  for number in range (5, 50, 5):
2      print (number)
  1. Expected: 5, 10 … 45, 50ten numbers
  2. Actual: 5, 10 … 45range stops before 50
  3. Line 1 is wrongthe stop value must be past 50
Corrected line: for number in range (5, 55, 5): (any stop value from 51 to 55 works).
Edexcel 1CP2 · 1.2.5

Correcting a logic error in given code

Paper 1 asks you to find a logic error in numbered code and write the corrected line.

  • Read the comment or the question to find what the code should do.
  • Trace with simple data until the result goes wrong.
  • Write the whole corrected line of Python, not just the changed symbol. Keep the rest of the line the same.
  • If the fix is to move a line (for example a total set to 0 inside a loop), say exactly where it should go.
Exam tipIn 2025 many students left “write the corrected line” blank. Even if you’re unsure, write your best attempt: a blank always scores 0.