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.

TrueFalseStartINPUT passcodepasscode!= 1978?OUTPUT WrongcodeINPUT passcodeOUTPUTUnlockedEnd

Output

 

Step 1: Run 1. A phone wants its passcode. The right code is 1978, and this person hasn't a clue.

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 wants its passcode. The right code is 1978, and this person hasn't a clue..

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 round it goes 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?' more than once. But exactly when does a while loop check its condition? People hold some very different ideas about this, and most of them sound reasonable.

Be honest. Which one is closest to what you think right now?
How sure are you?

Why bother with a loop?

Repeated if blocksvsOne while loop

Same guessing game, written two ways. Tap each row and see which one you'd rather write.

Focus

How many guesses?

Repeated if blocks

A fixed number: one 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 decided that before the game even started.

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 have run out, so the program carries on even though the guess is wrong.

One while loop

The loop asks again. It only ends when guess != number is False, which means 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. Two lines are missing. Your call.

  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.

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

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). 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?
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 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.