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?

TrueFalseStartINPUT passcodepasscode != 1978?OUTPUT Wrong codeINPUT passcodeOUTPUT UnlockedEnd

Output

 

Step 1: Run 1. A phone asks for its passcode. The right code is 1978, and this person doesn't know it yet.

1 / 16

Press Next to follow the arrows. Every arrow out of the loop body leads back up to the diamond.

Step 1 of 16: Run 1. A phone asks for its passcode. The right code is 1978, and this person doesn't know it yet..

Watch out: True doesn't mean 'right code'. The question is 'is passcode NOT equal to 1978?', so True means the code is still wrong, and the loop goes round again.

What do you really think?

When does the loop ask its question?

In the passcode program, the diamond asked 'is passcode not equal to 1978?' several times. But exactly when does a while loop check its condition? People hold some very different ideas about this.

Which is closest to what you think right now?
How sure are you?

Why bother with a loop?

Repeated if blocksvsOne while loop

A guessing game written two ways. Tap each row to see why it matters.

Focus

How many guesses?

Repeated if blocks

A fixed number: one extra guess for each if block you typed out.

One while loop

As many as it takes. It keeps asking while the guess is wrong.

The insight

This is the big one. The loop doesn't need to know in advance how many tries the player will need. The if version has decided before the game even starts.

How the checking works

Repeated if blocks

Each if checks the guess once, then the program moves on to the next line.

One while loop

One condition, while guess != number:, checked again at the start of every time round.

Still wrong after the last check?

Repeated if blocks

The ifs run out, and the program carries on even though the guess is wrong.

One while loop

Can't happen. The loop only ends when guess != number is False: a right guess.

Want to allow more guesses?

Repeated if blocks

Copy and paste another if block for every extra chance.

One while loop

Nothing to add. The loop already repeats as often as it needs to.

Now you build one

Three tries at the password

A login allows three attempts at the password sunflower. Build a loop that keeps asking while the guess is wrong and there are attempts left, and takes one attempt away after every wrong guess.

  1. password = "sunflower"
  2. attempts = 3The counter: how many attempts are left.
  3. guess = ""guess starts empty, so guess != password is True at the first check and the loop gets to run.
  4. missing step
Which line is step 4?

Your turn

Write the guessing game

A guessing game stores a secret number in the variable number. Write Python code that asks the user for a guess, then uses a while loop to keep asking for another guess for as long as the guess is not equal to number. Then explain how the loop's condition controls the repetition. [5 marks]

0 words · your answer stays on this page and is not sent anywhere.

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

1Iteration = repeating instructions in a loop.
2A condition is True or False. A while loop repeats while its condition is True.
3The condition is checked at the start of every time round, and only there.
4False: the loop stops and the next line after the loop runs.
5False at the very first check: the body runs zero times.
6Flowchart: the diamond is the condition; the True path loops back to it.
7A while loop beats repeated ifs: it keeps going until the answer is right.
8Join conditions with and; a counter that goes down can end the loop.

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?
Repeating a sequence of instructions, using a loop in a program.
What is a condition?
An expression that evaluates to True or False.
What is condition-controlled iteration?
Repeating a set of instructions based on whether a condition evaluates as True or False.
Which Python command gives condition-controlled iteration?
while. A while loop repeats its block of code as long as its condition is True.
When does a while loop check its condition?
At the start of every time round the loop.
A while loop's condition comes out False. What does the program do?
It stops repeating and carries on from the first instruction that follows the loop.
A while loop's condition is False the first time it's checked. How many times does the body run?
Zero. The code inside the loop does not run at all.
How is condition-controlled iteration shown in a flowchart?
With a decision diamond. If True, the loop's blocks run and the flow goes back to the decision.
What does != mean?
'Is not equal to'. passcode != 1978 is True whenever passcode is anything other than 1978.
Why use a while loop instead of repeated if blocks for a guessing game?
It's more efficient: it allows guessing until the right number is entered, instead of a fixed number of attempts written out as selection.
while guess != password and attempts > 0: when does this loop keep going?
Only while both parts are True: the guess is wrong AND there are attempts left. Either one turning False stops it.
In a password loop with a limited number of attempts, what does the counter do?
It goes down by one after each wrong guess, so it can reach 0 and end the loop when the user runs out 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 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.