GCSE · Computer Science · Edexcel · Spec 1CP2

Trace tables

Can you tell by looking at code how many times a loop runs and what it prints? It's easy to get wrong. A trace table turns the guess into proof.

Computer Science · Algorithms

Trace table — dry run the code

Be the computer. Before each click, guess which cell gets filled next — then check.

1value = 3
2steps = 0
3while value < 20:
4 value = value * 2
5 steps = steps + 1
6print(steps)
Line
value
steps
value < 20
Output
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

Reading the table

What do the rows and columns actually mean?

Think back to the trace you just stepped through: a 6-line program, 13 rows, and lots of empty cells.

Which is closest to what you think right now?
How sure are you?

Your turn

Finish the trace

This is a new program. The trace is filled in up to each gap — you choose what goes in the next row.

  1. 1 lives = 2 2 while lives > 0: 3 print(lives) 4 lives = lives - 1Lines 3 and 4 are inside the loop. Columns: Line, lives, lives > 0, Output.
  2. Line 1 · lives = 2
  3. Line 2 · lives > 0 is True
  4. Line 3 · OUTPUT 2
  5. missing step
Which line is step 5?

Exam line: To update a variable, look up its current value in the table, work out the expression, and write the result as the new value.

Predict, then check

Trace it in your head, or on scrap paper, before you commit.

count starts at 0. A while loop keeps running while count != 10 (while count is not equal to 10), and every pass adds 4 to count. What happens?

Spot the slip

When the trace is wrong, not the program

The program: Line 1: total = 0 · Line 2: n = 4 · Line 3: while n > 0: · Line 4: total = total + n · Line 5: n = n - 2 · Line 6: print(total). Lines 4 and 5 are inside the loop. A student traced it by hand and got an output of 2. Pick the row where their trace goes wrong.

A student's hand-written trace — which line goes wrong?

Put it into words

Why bother with a trace table?

Explain two reasons why a programmer might use a trace table, and give one limitation of using one. [3 marks]

0 words · your answer stays on this page and is not sent anywhere.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Run an algorithm by hand, one step at a time, and find out what it really does.

What you need to know

  • Set up a trace table with the columns a particular algorithm needs.
  • Fill it in one execution step at a time, updating each variable from its current value.
  • Record every loop-condition result and use it to say when — or whether — a loop stops.
  • Explain why programmers use trace tables, and their main limitation.

The big picture

A trace table lets you run an algorithm by hand, without a computer. Its columns hold what you're tracking — the line number, each variable, each condition and the output — and each row is one step the algorithm actually takes. You write a value only when it changes, or when a condition or output is evaluated, and you record every True or False from a loop condition. The finished table shows what the algorithm does, reveals a loop that never ends and helps pinpoint logic errors — but because it's filled in by hand, it can contain mistakes of its own.

Key points

1A trace table records the variables, the outputs and the result of each condition at every step of an algorithm run by hand.
2Columns are the things you track (line, variables, conditions, output) and depend on the algorithm; rows are steps of execution, not lines of code.
3Write in a column only when its value changes, or when a condition or output is evaluated or produced.
4To trace an update such as count = count + 4, look up count's current value, work out the expression and write the result as the new value.
5A while loop checks its condition before every pass and stops only when the condition is False; if the traced values skip past what the condition waits for, the loop never ends.
6Trace tables give a clear step-by-step view of an algorithm and help pinpoint logic errors, but they're done by hand, so they can contain human errors — especially for long or complex algorithms.

Worked example

Problem

Trace this algorithm and give its output. Line 1: stock = 14 · Line 2: sold = 0 · Line 3: while stock >= 5: · Line 4: stock = stock - 5 · Line 5: sold = sold + 1 · Line 6: print(stock). Lines 4 and 5 are inside the loop.

⚠ Watch out

Giving each line of code one row. A line inside a loop runs again on every pass, so it needs a new row every time — and that includes every check of the loop condition, not just the first.

🧠

Memory hook

Be the computer: one step, one row. Look it up, work it out, write it down — and the loop only stops when the condition says False.

✓

Check yourself

Trace x = 1, then while x < 30: x = x * 3. How many condition checks happen, and what is x at the end? (5 checks: four True, one False. x = 81.)

Flashcards

(13)
What is a trace table?
A table for tracking the values of variables and the flow of execution as you run an algorithm by hand, step by step, without a computer.
What does a trace table record at each step?
The state of the variables, any output, and the result (True or False) of any condition that is checked.
What do the columns of a trace table show?
The things being tracked — such as the line number, each variable, each condition and the output. The headings depend on the algorithm.
What does each row of a trace table represent?
One step in the execution of the algorithm. So the number of rows depends on how many steps it takes, not how many lines of code it has.
When do you write a value in a column?
Only when it changes — or, for a condition or an output, when it is evaluated or produced.
How do you trace the line total = total + 7?
Look up total's current value in the table, add 7, and write the result as total's new value. It replaces the old one.
When is a while loop's condition checked?
Before every pass through the loop — and the True or False result is recorded each time.
When does a while loop stop?
When its condition evaluates to False. Execution then carries on with the first line after the loop.
How does a trace table show a loop that never ends?
The variable in the condition keeps changing but the condition never comes out False — for example, the variable steps straight past the value the condition is waiting for.
A loop's counter jumps over the value its condition is waiting for. How can you fix it?
Change the condition so it can become False — for example, a less-than-or-equal-to test instead of 'is not equal to'.
How does a trace table help find logic errors?
It shows how values and condition results change at every step, so you can pinpoint where the logic or a calculation goes wrong before it becomes a bigger problem in the code.
Why does a trace table help you understand an algorithm?
It gives a clear, structured, step-by-step view of how the algorithm processes data and how its variables change over time.
What is the main limitation of trace tables?
They're completed by hand, so they can contain human errors — especially when tracing long or complex algorithms.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 29 September 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.