GCSE · Computer Science · Edexcel · Spec 1CP2
Condition-controlled repetition
Type the wrong code into a locked phone and it just asks again. And again. It has no idea how many tries you'll need. That's a while loop.
Watch the diamond
The passcode loop
Two people, one program. Step through both runs and keep your eye on the diamond. In Python, that diamond is the line while passcode != 1978:
Before you press Next: in Run 1 the person types 1234, then 2020, then 1978. How many times will the diamond ask its question? Commit to a number.
Output
Step 1: Run 1. A phone wants its passcode. The right code is 1978, and this person hasn't a clue.
Press Next to follow the arrows. Every arrow out of the loop body leads back up to the diamond.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
One question, asked again and again. The answer decides whether the loop goes round once more.
What you need to know
- Iteration means repeating a sequence of instructions. A program does it with a loop.
- A condition is an expression that comes out as either True or False. Nothing in between.
Have a goQuick one. Is 7 > 3 a condition? If it is, what does it come out as?
Yes. It comes out True.
It can only ever be True or False, and 7 really is bigger than 3. A plain number like 7 on its own isn't a condition, because it isn't True or False.
- In condition-controlled iteration, a condition decides whether the instructions get repeated again.
- Python does this with the while command: a while loop repeats its block for as long as its condition is True.
- The condition is checked at the beginning of each time round the loop.
- Once a check comes out False, the loop stops and the program runs the next line after the loop.
Have a gototal = 1, then while total < 20: total = total * 3. What is total when the loop has finished? Work it out before you peek.
27
total goes 1, 3, 9, 27. At 9 the check 9 < 20 is still True, so the body runs once more. The loop only stops at the next check, when 27 < 20 is False. It never stops at 20.
- If the condition is False the very first time it's checked, the code inside the loop doesn't run at all.
Have a goYour mate writes count = 8, then while count < 3: print("Hi"). They bet you a biscuit it prints Hi once. Do they win the biscuit?
No. Keep your biscuit: it prints nothing.
8 < 3 is False at the very first check, so the body is skipped. A while loop doesn't give its body a free run before the check.
- In a flowchart the condition sits in a decision diamond. True runs the loop's blocks, then the flow goes back to the diamond.
- Swapping repeated if blocks for a while loop is more efficient: guessing carries on until it's right, not for a fixed number of tries.
- A condition can join two parts with and, plus a counter that drops by one per wrong guess. Right guess or zero attempts left: loop over.
The big picture
Repeating a set of instructions in a program is called iteration, so condition-controlled repetition is just condition-controlled iteration: a condition (something that comes out True or False) decides whether the instructions go round again. In Python that's the while loop. It checks its condition at the start of every time round. True: run the body, then come back and ask again. False: leave the loop and carry on with the next line after it. False at the very first check means the body never runs at all. In a flowchart the condition is the decision diamond, and the True path loops back up to it.
Key points
Worked example
Problem
Turn the passcode flowchart from the top of this page into a Python program. The right passcode is 1978.
⚠ Watch out
Writing the condition for ENDING the loop instead of the one that keeps it GOING. The passcode loop needs while passcode != 1978: (keep asking while it's wrong). Write while passcode == 1978: and typing 1234 makes the first check False, so the loop is skipped and the phone unlocks.
Memory hook
Ask at the top, every time round. True: go round again. False: move on.
Check yourself
Without scrolling up: what question does the passcode loop ask, and when? What's printed if the first code typed is 1978? And why does the loop body need its own input line?
Flashcards
(12)What is iteration?
What is a condition?
What is condition-controlled iteration?
Which Python command gives condition-controlled iteration?
When does a while loop check its condition?
A while loop's condition comes out False. What does the program do?
A while loop's condition is False the first time it's checked. How many times does the body run?
How is condition-controlled iteration shown in a flowchart?
What does != mean?
Why use a while loop instead of repeated if blocks for a guessing game?
while guess != password and attempts > 0: when does this loop keep going?
In a password loop with a limited number of attempts, what does the counter do?
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.