GCSE · Computer Science · Edexcel · Spec 1CP2
Programming – condition-controlled repetition
Bet you can't spot it: a loop starts at 4, adds 4 each lap and runs while count != 10. Does it ever stop?
Computer Science · Algorithms
Trace table — dry run the code
Press Next step and watch two things: the count column, and the check on the while line. The loop says: keep going while count != 10, which means while count is NOT 10.
Ready when you are — step through one line at a time.
Computer Science · Flowcharts
Where does the loop-back arrow come from?
Walk the path with the step buttons. At the diamond, watch which way the program goes and why.
Follow the count from 1. Each time you reach the diamond, ask: True or False?
Output
Step 1: The program begins.
Step through the trace — every value is the lesson’s, not run by the page.
Say each one out loud before you flip it
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
What you need to know
- Iteration means repeating a sequence of instructions, and a loop statement is how a program does it.
- A condition is an expression that evaluates to True or False.
- In Python, condition-controlled iteration is a while loop, which repeats actions while its condition is True.
Have a goYour mate Sam says a while loop "keeps going until its condition is True". Sam sounds very sure. Fix the sentence.
It keeps going while its condition is True.
"Until" flips the meaning. The loop repeats while the condition is True, and the moment it is False the loop is over.
- The condition is checked at the beginning of each iteration, before the loop body runs.
- If the condition fails at the start, the body never runs, so a while loop does not always run at least once.
Have a goA loop is written as while lives > 0: and lives starts at 0. How many times does the body run?
Zero times.
The check comes before the first lap, and 0 > 0 is False. A while loop does not always run at least once.
- When the condition becomes False, the loop stops and the program carries on with the next line after the loop.
- In a flowchart, a decision diamond holds the condition: True runs the body and loops back, False runs the next line.
- A trace table tracks variable values and the flow of execution step by step, and each row is one step.
Have a goTrace it: x starts at 10, and the loop is while x > 4: with x = x - 3 inside. What is x when the loop stops?
4
10 becomes 7 (7 > 4 is True), then 4. The next check asks 4 > 4, which is False, so the loop stops with x = 4.
- A trace table can expose a logic error: with count going up by 4, count != 10 never becomes False.
- A loop condition can combine tests with and, plus a counter, to stop on a right answer or a maximum number of attempts.
The big picture
A while loop repeats its instructions while its condition is True, and it checks that condition at the start of every iteration. That means it can run zero times, or never stop at all. A trace table shows you exactly why.
Key points
Worked example
Problem
Trace this program and say what it prints: n = 3, total = 0, then while n > 0: total = total + n and n = n - 1. After the loop comes print(total).
⚠ Watch out
Reading while as 'until'. A while loop repeats while its condition is True. And because the check comes first, a loop whose condition is False at the start never runs its body at all.
Memory hook
Ask first, loop second: a while loop checks its question before every lap, and it only goes round while the answer is True.
Check yourself
Cover the page. Why might a while loop run zero times? And what would a trace table show for a loop that never stops?
Flashcards
(13)What is iteration?
What is a condition?
What is condition-controlled iteration?
What is a while loop?
When is a while loop's condition checked?
Does a while loop always run at least once?
What happens when a while loop's condition becomes False?
How is condition-controlled iteration drawn in a flowchart?
What is a trace table?
What does each row of a trace table show?
With count starting at 4 and adding 4, why does while count != 10 never stop?
Why use a while loop to ask for a password?
How can a loop stop on a right answer or after a maximum number of attempts?
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
- Decomposition and abstraction in programming
- Evaluating program fitness and efficiency
- Functions vs procedures
- Global vs local variables
- Identifying and correcting program errors
- Primitive data types
- Programming – count-controlled repetition
- Programming – iteration over data structures
- Programming – selection
- Programming – sequencing
- Programming arithmetic operators
- Programming logical operators
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 14 November 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.