GCSE · Computer Science · Edexcel · Spec 1CP2
Determining algorithm output
Type 6 into a program and it prints something. You could guess what. Or you could be the computer: one line at a time, writing down what changes.
Computer Science · Algorithms
Trace table — dry run the code
This program is meant to output the numbers divisible by three. The user types 6. Be the computer: run one line, write down what that line did, then run the next.
Ready when you are — step through one line at a time.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Don't guess what a program prints. Walk it, one line at a time, and write down everything that changes.
What you need to know
- Walking through an algorithm and checking its variables shows how it works, and helps you catch and fix errors.
- A trace table is how you write that walk down: variables, condition results and outputs, step by step.
- Columns hold what you track: the line, the variables, the conditions and the outputs. Their number and names change with each algorithm.
- Each row is one step of the execution, so you can watch the values change over time.
Have a goYour friend reckons a loop that runs four times needs only one row in the trace table, because it's just one line of code. Convinced?
Nope. That line gets a new row every time it runs: four passes, four rows.
Rows are steps of the execution, not lines of code, so a line inside a loop is recorded again on every pass.
- Begin by identifying the variables and putting them in the column headings.
- For each line: record any assignment, record True or False for any condition, and record any output.
- A False condition means the line it controls is skipped this time round. A while loop is revisited while its condition is True.
Have a goA trace reaches if x > 10: print(x) with x = 7. What do you write for the condition, and is anything printed?
False, and nothing is printed this time round.
7 > 10 is False, so the print line it controls is skipped. You record the condition's True/False result, not the 7.
- % (modulo) gives the remainder, so 14 % 4 is 2. // (integer division) gives the whole number of times, so 15 // 4 is 3.
Have a goQuick one before you read on: what are 20 % 6 and 20 // 6?
20 % 6 is 2, and 20 // 6 is 3.
6 goes into 20 three whole times (18), leaving 2. // gives the 3, % gives what's left over, and neither gives the decimal 3.33.
- Trace tables work on flowcharts, pseudocode and program code, not just pseudocode.
- Pros: a clear step-by-step view, and they pinpoint logic errors. Cons: human error, and they're impractical for complex algorithms.
The big picture
To find an algorithm's output, you trace it: walk through it one step at a time, recording the state in a trace table. Columns track the variables, conditions and outputs, and each row is one step. You write down each assignment, each condition's True or False, and each output. A False condition skips the line it controls this time round. You'll need % (remainder) and // (whole-number division) too. The method works on flowcharts, pseudocode and code. It's clear and pinpoints errors, but it's open to human error and impractical for complex algorithms.
Key points
Worked example
Problem
Trace this program and state its output. num = 472, total = 0, then while num > 0: total = total + num % 10, and num = num // 10. After the loop: print(total).
⚠ Watch out
Stopping the trace one row too early. A while loop keeps going back to its condition while it's True, so the check that finally comes out False gets its own row (2 > 2 is False). Skip it and you can easily run the loop once too often or too few.
Memory hook
Be the computer: one line, one row. True? Go in. False? Skip it this time round. And % is whatever's left over.
Check yourself
Trace it on paper: x = 10, then while x > 4: x = x - 3, then print(x) after the loop. What is output, and what does your last row say about the condition?
Flashcards
(14)What is a trace table?
In a trace table, what do the columns represent?
In a trace table, what does each row represent?
What is the first thing to do when using a trace table to find an output?
A line contains a condition. What goes in the trace table?
What happens to the line controlled by a condition that is False?
When is a while loop revisited?
What does the % (modulo) operator give?
What does the // (integer division) operator give?
What is the test num % 3 == 0 checking?
Can trace tables only be used on pseudocode?
Why is walking through an algorithm useful?
Give two advantages of trace tables.
Give two limitations 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 28 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.