GCSE · Computer Science · Edexcel · Spec 1CP2
Trace tables
How many times does that loop run, and what does it print? Guessing from the code is 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.
Ready when you are — step through one line at a time.
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?
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
- A trace table lets you run an algorithm by hand, with no computer, following the variables and the flow of execution step by step.
- At every step it records the state of the variables, any output, and the True or False result of each condition checked.
- Columns are what you track: line number, each variable, each condition, the output. Which ones you need depends on the algorithm.
- Each row is one step of execution, not one line of code. A line inside a loop gets a fresh row on every pass.
Have a goYour mate insists a 4-line program can never need more than 4 rows in its trace table. The program has a loop in it. Win the argument in one sentence.
The loop's lines run again on every pass, and each run is a new row.
Rows count the steps the computer takes, not the lines on the page, so a loop that goes round three times adds rows on every lap.
- Write a value only when it changes, or for a condition or output, when it's evaluated or produced. Empty cells are fine.
- For an update like count = count + 4, look up count's current value, work it out, and write the result as the new value.
Have a goThe total column currently reads 7. The next line to run is total = total + 5. What do you write?
12
Start from the current value (7) and write the result. Writing 5 records the amount added, not what total now is.
- A while loop checks its condition before every pass, records True or False each time, and stops only when the check is False.
- If the traced values jump past what the condition is waiting for, it never becomes False and the loop never ends. Fix the condition.
Have a gon starts at 1, goes up by 2 each pass, and the loop runs while n != 9. Does this loop ever stop? Trace it before you decide.
Yes. n goes 1, 3, 5, 7, 9, so n != 9 is False at the fifth check.
Not every != loop is a trap. The values never skip past 9, they land on it exactly, so the check finally comes out False.
- Trace tables give a clear step-by-step view of an algorithm and help you pinpoint logic errors before they grow into bigger problems.
- The catch: they're filled in by hand, so they can contain human errors, especially for long or complex algorithms.
The big picture
A trace table lets you run an algorithm by hand, with no computer. The columns are what you track (line number, each variable, each condition, 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 or produced, and you record every True or False from a loop condition. The finished table shows what the algorithm does, exposes a loop that never ends and helps you pinpoint logic errors. The catch: it's filled in by hand, so it can contain mistakes of its own.
Key points
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. Looks sensible. It isn't. 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?
What does a trace table record at each step?
What do the columns of a trace table show?
What does each row of a trace table represent?
When do you write a value in a column?
How do you trace the line total = total + 7?
When is a while loop's condition checked?
When does a while loop stop?
How does a trace table show a loop that never ends?
A loop's counter jumps over the value its condition is waiting for. How can you fix it?
How does a trace table help find logic errors?
Why does a trace table help you understand an algorithm?
What is the main limitation of trace tables?
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 postedMore Edexcel GCSE Computer Science topics
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 26 October 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.