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.

1num = int(input())
2while num > 2:
3 if num % 3 == 0:
4 print(num)
5 num = num - 1
num
num > 2
num % 3 == 0
Output
Press Start to run the first line.
·

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

Before you trust your trace

What does % actually give you?

The hero program kept asking num % 3 == 0. Get % wrong and every row after it goes wrong too. So let's check what you really think it does.

In Python, what does 14 % 4 give?
How sure are you?

Your turn: fill the gaps

Trace a flowchart

A flowchart sets count = 1 and total = 0. Then a decision asks: is count < 5? If Yes: total = total + count, then count = count + 1, and back to the decision. If No: output total. Fill in the missing rows of the trace.

  1. Start · count = 1 · total = 0
  2. 1 < 5 → True · total = 0 + 1 = 1 · count = 2
  3. missing step
Which line is step 3?

Spot the slip

Where does this trace go wrong?

The program: n = 1, then while n < 20: n = n * 2, and after the loop print(n). A student traced it row by row. One row is wrong. Which one?

A student's trace — which line goes wrong?

Your verdict

Is a trace table the best way to find an output?

The claim

A trace table is the best way to find out what an algorithm outputs.

Place each piece of evidence to load the balance. Mark the strong ones — they count double.

  1. It gives a clear, step-by-step picture of how the algorithm processes the data.

    Evidence 1: does it support or challenge the claim?
  2. It helps you pinpoint an error in the logic or a calculation before it grows into a bigger problem.

    Evidence 2: does it support or challenge the claim?
  3. It works on flowcharts, pseudocode and program code alike.

    Evidence 3: does it support or challenge the claim?
  4. It is filled in by a person, so it is open to human error.

    Evidence 4: does it support or challenge the claim?
  5. It becomes impractical for a long or complex algorithm.

    Evidence 5: does it support or challenge the claim?

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

  • A trace table tracks the values of variables and the flow of execution, step by step, as you mentally run an algorithm.
  • Columns hold the important things to track (the line, the variables, the conditions and the outputs). Each row is one step of the execution.
  • For each line: record any variable assignment, record True or False for any condition, and record any output.
  • When a condition is False, the line it controls is skipped this time round. A while loop is revisited while its condition is True.
  • % (modulo) gives the remainder, so 14 % 4 is 2. // (integer division) gives the whole number of times, so 15 // 4 is 3.
  • Trace tables work on flowcharts, pseudocode and program code. They give a clear step-by-step view and help pinpoint logic errors, but they're open to human error and 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

1Trace, don't guess: one row per step, and the output falls out at the end.
2Start by identifying the variables and making them your column headings, adding columns for conditions and outputs.
3A condition's row says True or False. False means the line it controls is skipped this time round.
4A while loop keeps going back to its condition while it is True, so the check that finally comes out False is the last row of the trace.
5% is what's left over, // is how many whole times: 6 % 3 is 0, and 7 // 3 is 2.
6The same method traces a flowchart, pseudocode or program code.
7Strengths: a clear view and it pinpoints errors. Limits: human error, and it's impractical for complex algorithms.

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 times.

🧠

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?
A tool for tracking the values of variables and the flow of execution through an algorithm, step by step.
In a trace table, what do the columns represent?
The important things to track: the line, the variables, the conditions and the outputs. The names and number of columns vary with each algorithm.
In a trace table, what does each row represent?
One step in the execution of the algorithm.
What is the first thing to do when using a trace table to find an output?
Identify the variables and add them to the column headings.
A line contains a condition. What goes in the trace table?
The result of evaluating it: True or False.
What happens to the line controlled by a condition that is False?
It is skipped this time round.
When is a while loop revisited?
Whenever its condition is True. It stops when the condition is checked and comes out False.
What does the % (modulo) operator give?
The remainder when one number is divided by another. For example, 6 % 3 gives 0.
What does the // (integer division) operator give?
The whole number of times the divisor goes into the dividend. For example, 7 // 3 gives 2.
What is the test num % 3 == 0 checking?
Whether 3 goes into num exactly, leaving no remainder.
Can trace tables only be used on pseudocode?
No. They work on flowcharts, pseudocode and program code.
Why is walking through an algorithm useful?
It shows how the algorithm works, and it helps you detect and correct errors.
Give two advantages of trace tables.
They give a clear step-by-step view of how the algorithm processes data, and they help pinpoint errors in logic or calculations.
Give two limitations of trace tables.
They are open to human error, and they can be impractical for 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.