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 never knows 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 it's 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?
Output
Step 1: Run 1. A phone asks for its passcode. The right code is 1978, and this person doesn't know it yet.
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.
A while loop never knows how many times it will run. It just keeps asking the same question.
What you need to know
- Iteration means repeating a sequence of instructions. A program does it with a loop, which can repeat a set of instructions.
- A condition is an expression that evaluates to True or False. In condition-controlled iteration, a condition decides whether the instructions are repeated.
- In Python, condition-controlled iteration uses the while command: a while loop repeats its block of code as long as its condition is True, for example while passcode != 1978:
- The condition is checked at the beginning of each loop. When it is False, the loop stops and the program carries on with the next line of code after the loop.
- The loop only runs if its condition allows it: if the condition is False the first time it is checked, the code inside the loop does not run at all.
- In a flowchart, the condition is a decision block (a diamond). If it is True, the blocks inside the loop run and the flow goes back to the decision.
- Replacing repeated if blocks with a while loop is more efficient: the loop allows guessing until the answer is correct, instead of a fixed number of attempts written out as selection.
- A while condition can join two conditions with and, and can use a counter. In a password loop that continues while the guess is wrong and attempts are left, attempts goes down by one after each wrong guess, and the loop stops when the user gets it right or runs out of attempts.
The big picture
Repeating a set of instructions in a program is called iteration, so condition-controlled repetition is the same thing as condition-controlled iteration. In condition-controlled iteration, a condition (an expression that is either True or False) decides whether the instructions run again. In Python this is the while loop: while the condition is True, the loop body repeats. The condition is checked at the start of every time round; as soon as it is False, the loop stops and the program runs the next line after the loop. If it's False at the very first check, the body never runs. In a flowchart, the condition is the decision diamond, and the True path loops back 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). With while passcode == 1978:, typing 1234 makes the very 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 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.