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.

1count = 4
2while count != 10:
3 print(count)
4 count = count + 4
count
count != 10
Output
Press Start to run the first line.
·

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?

TrueFalseStartcount = 1count < 4 ?print(count)count = count + 1print("Done")End

Output

 

Step 1: The program begins.

1 / 14

Step through the trace — every value is the lesson’s, not run by the page.

Step 1 of 14: The program begins..

Computer Science · While loops

Does this loop run at all?

A game sets lives = 0 and then runs: while lives > 0: print("Still alive"). After the loop comes print("Game over"). Three friends predict what appears on screen.

Whose prediction is closest to yours?
How sure are you?

Asking for a password

Repeated if-else blocksvsA while loop

Same job, two tools. Look at what happens when the user keeps getting it wrong.

Focus

How many attempts does the user get?

Repeated if-else blocks

A fixed number: one per if-else block you write.

A while loop

As many as it takes. It keeps asking while guess != password.

The insight

This is the big difference. Selection can only ask as many times as you wrote blocks. The loop asks again for as long as the condition stays True.

How much code?

Repeated if-else blocks

Copy the whole block again for every extra attempt.

A while loop

One short loop, written once.

When does it stop asking?

Repeated if-else blocks

After the last block, whether or not the password was right.

A while loop

When guess != password becomes False, which happens when the correct password is entered.

Computer Science · While loops

Build the password loop

The user gets up to 3 attempts at a password. The loop must stop when they get it right OR when they have used all 3 attempts. Build it line by line.

  1. password = "tiger"
  2. guess = ""The condition is checked before the body runs, so guess must already exist. Starting it as an empty string means it is wrong at the first check, and the loop will run.
  3. missing step
Which line is step 3?

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

1A while loop repeats while its condition is True, and the condition is checked at the start of each iteration.
2If the condition is False at the start, the body runs zero times, so a while loop does not always run at least once.
3When the condition becomes False, the loop stops and the next line after the loop runs.
4A trace table shows the variable values and the condition's result at each step, which exposes a loop that never becomes False.

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?
Repeating a sequence of instructions within a program loop. A loop statement is how a program repeats instructions.
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.
What is a while loop?
How Python does condition-controlled iteration: it repeats actions while its condition is True.
When is a while loop's condition checked?
At the beginning of each loop, before each iteration. Variable changes inside the loop affect whether it continues or stops.
Does a while loop always run at least once?
No. If the condition is not met at the start, the code inside the loop does not run at all.
What happens when a while loop's condition becomes False?
The loop stops and the program executes the next line of code after the loop.
How is condition-controlled iteration drawn in a flowchart?
With a decision (diamond). True: the block of code runs, then it loops back to the condition. False: the next line of code runs.
What is a trace table?
A tool that tracks variable values and the flow of execution step by step. Columns can hold the line, variables, conditions and outputs.
What does each row of a trace table show?
One step in the execution.
With count starting at 4 and adding 4, why does while count != 10 never stop?
count goes 4, 8, 12 and skips 10, so count != 10 is never False and the loop never breaks.
Why use a while loop to ask for a password?
while guess != password keeps asking until the correct one is entered, which is more efficient than repeated if-else blocks that give a fixed number of attempts.
How can a loop stop on a right answer or after a maximum number of attempts?
Combine tests with a logical operator such as and, together with a counter variable that changes inside the loop.

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