GCSE · Computer Science · AQA · Spec 8525

Indefinite (condition-controlled) iteration

How many goes should a program give you to type your password? Nobody knows until you get it right. So you need a loop that doesn't count, but keeps checking.

Computer Science · Iteration

Watch the condition do the deciding

Step through this WHILE loop one line at a time. Keep an eye on the WHILE line: control keeps coming back up to it, and each time it asks, 'Shall I go round again?'

Before you press Next: how many numbers will this loop output?

1 n ← 10
2 WHILE n < 50
3 OUTPUT n
4 n ← n * 2
5 ENDWHILE
6 OUTPUT 'Done'

Variables

n=10pass=0last check=—

Output

 

Step 1: n starts at 10. Nothing has been checked yet.

1 / 12

Press Next and watch where the highlight goes after each doubling. It always climbs back to the WHILE line.

Step 1 of 12: n starts at 10. Nothing has been checked yet..

Exam line: A WHILE loop checks its condition before every pass. It keeps going while the condition is TRUE and stops on the first check that comes out FALSE.
Watch out: The loop doesn't stop the moment n becomes 80. It only notices when control climbs back to the WHILE line and checks again.

Predict, then check

Both loops have the same body: OUTPUT n, then n ← n * 2. Both start with n ← 80.

Loop A is WHILE n < 50 … ENDWHILE. Loop B is REPEAT … UNTIL n ≥ 50. Starting from n ← 80, how many times does each body run?

Computer Science · Iteration

What do you really think?

A quiz program uses REPEAT … UNTIL Correct to keep asking a question until the player gets it right.

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

Exam line: UNTIL stops when its condition becomes TRUE. WHILE keeps going while its condition is TRUE. So REPEAT … UNTIL Solved and DO … WHILE NotSolved do the same job.

Computer Science · Algorithms

Trace table — dry run the code

A FOR loop sits inside a WHILE loop. Predict first: when total goes past 20 halfway through the second round, does the outer loop stop straight away?

1rounds ← 0
2total ← 0
3WHILE total < 20
4 FOR i ← 1 TO 5
5 total ← total + i
6 ENDFOR
7 rounds ← rounds + 1
8ENDWHILE
9OUTPUT rounds
rounds
total
i
total < 20?
Output
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

Computer Science · Writing algorithms

Finish the program

Write an algorithm that asks for a number from 1 to 10. It should keep asking until the entry is in range, saying 'Too high' or 'Too low' after each bad entry.

  1. CONSTANT MAX ← 10A constant: a named value that never changes while the program runs.
  2. num ← USERINPUTAssignment: grab the first entry before the loop tests it. WHILE checks first, so num needs a value already. If it's in range, the loop runs zero times.
  3. missing step
Which line is step 3?

Exam line: A condition-controlled loop needs something that can change its condition as it runs, usually a line in its body. Here that's a fresh USERINPUT. Without it, one bad entry would keep the loop going forever.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

What you need to know

  • Indefinite (condition-controlled) iteration repeats until a condition says stop, so the number of passes isn't fixed before the loop starts.
  • Indefinite doesn't mean infinite. Once you know the data, you can trace exactly how many passes happen.
  • WHILE … ENDWHILE has its condition at the start. It checks before every pass, so the body can run zero times.
  • REPEAT … UNTIL and DO … WHILE have their condition at the end. The body runs first, so it always runs at least once.
  • Have a goA game must show its menu at least once before asking if you want to quit. Should the loop's condition go at the start or the end?

    At the end: REPEAT … UNTIL or DO … WHILE.

    A check at the end guarantees one run of the body, so the menu always appears. A check at the start could skip the menu entirely if the condition were already met.

  • Careful with the keywords: UNTIL stops when its condition is TRUE, but WHILE keeps going while its condition is TRUE.
  • Have a goYour mate swaps REPEAT … UNTIL Finished for DO … WHILE Finished and says, 'Same thing, fancier word.' After the first pass, what does their loop actually do?

    It carries on only while Finished is TRUE, so it quits while you're still unfinished.

    WHILE keeps going on TRUE, but UNTIL stops on TRUE. Keeping the same condition with the other keyword turns the loop's behaviour back to front.

  • That's why REPEAT … UNTIL Solved does the same job as DO … WHILE NotSolved. Swap the keyword, flip the condition.
  • You need to understand checks at both ends, even if your programming language only gives you one kind.
  • Something inside the loop, such as fresh input or an updated variable, must be able to change the condition. Otherwise it may never stop.
  • Have a golives ← 3, then WHILE lives > 0, OUTPUT 'Ouch', ENDWHILE. What line is missing from the body?

    Something like lives ← lives - 1.

    Nothing changes lives, so 3 > 0 is TRUE at every check and 'Ouch' never ends. A line that changes lives lets the condition eventually come out FALSE.

  • In nested loops, the inner loop runs all its passes on each outer pass. The outer condition is rechecked after it finishes.
  • Loops combine with constants, variables, assignment, selection and subroutines, built from three principles: sequence, selection and iteration.

The big picture

A condition-controlled loop is steered by a condition, not a count: the number of passes isn't fixed before it starts, but with known data you can trace it exactly, so indefinite isn't infinite. A check at the start (WHILE … ENDWHILE) allows zero passes; a check at the end (REPEAT … UNTIL or DO … WHILE) guarantees at least one. UNTIL stops on TRUE, WHILE carries on while TRUE, and something must be able to change the condition. Loops nest, and combine with constants, variables, assignment, selection and subroutines.

Key points

1A condition-controlled loop checks its condition on every pass, not just once. Indefinite means the count isn't fixed in advance, not that it runs forever.
2Condition at the start: zero passes are possible. Condition at the end: at least one pass is guaranteed.
3UNTIL means 'stop when TRUE'; WHILE means 'keep going while TRUE', so converting between them means flipping the condition.
4Something must be able to change the condition as the loop runs, usually a line in the body such as fresh input or an updated variable. If nothing can, a loop that starts never stops.
5In a nested loop, the outer condition is only checked again after the inner loop has finished.
6Sequence, selection and iteration are the three combining principles of high-level imperative programming.

Worked example

Problem

Trace this algorithm and state exactly what it outputs. x ← 1 REPEAT x ← x * 3 OUTPUT x UNTIL x > 20

⚠ Watch out

Forgetting to change the tested variable inside the loop. It looks finished, but if nothing in the body can change what the condition tests, a loop that starts running never stops.

🧠

Memory hook

WHILE looks before it leaps; REPEAT leaps, then looks. And UNTIL is the impatient one: the moment its condition is TRUE, it's out.

✓

Check yourself

count is 5 and neither loop body changes it. How many times does WHILE count < 3 … ENDWHILE run its body? And REPEAT … UNTIL count ≥ 3?

Flashcards

(14)
What is indefinite (condition-controlled) iteration?
A loop that keeps repeating until a condition tells it to stop. The number of passes isn't fixed before it starts: the data decides.
Does 'indefinite' mean the loop runs forever?
No. Indefinite means nobody fixed the count in advance; with known data you can trace exactly how many passes happen. Infinite means it never stops at all.
WHILE … ENDWHILE: where is the condition, and what does that mean?
At the start. It is checked before every pass, so if it is FALSE the first time, the body runs zero times.
REPEAT … UNTIL: where is the condition, and when does it stop?
At the end. The body always runs at least once, and the loop stops when the condition becomes TRUE.
DO … WHILE: where is the condition, and when does it stop?
At the end. The body always runs at least once, and the loop keeps going while the condition is TRUE, stopping when it becomes FALSE.
Which DO … WHILE loop does the same job as REPEAT … UNTIL Solved?
DO … WHILE NotSolved. UNTIL stops when its condition is TRUE and WHILE continues while its condition is TRUE, so the condition has to flip.
A WHILE loop runs its body 3 times. How many times was its condition checked?
Before every pass, plus one last time: the check that comes out FALSE and ends the loop. So 3 passes means 4 checks.
Why might a condition-controlled loop never stop?
Nothing inside the body can change what the condition tests, so it gives the same answer every time. The body needs something like fresh input or an updated variable.
What is nested iteration?
A loop placed inside the body of another loop. The inner loop runs right through, start to finish, on each pass of the outer loop.
A FOR loop is nested inside a WHILE loop. When is the WHILE condition checked again?
When control gets back to the WHILE line, which is after the inner loop has finished its passes, not halfway through them.
What are the three combining principles of high-level imperative programming?
Sequence (steps in order), selection (choosing a path) and iteration (repetition).
Which other statement types can iteration be combined with in a program?
Variable declaration, constant declaration, assignment, selection and subroutines.
Do you need to understand checks at both ends of a loop if your language only has one kind?
Yes. Whether the check comes before or after the body is an idea about how loops behave, and you should be able to reason about both kinds whatever language you use.
When would you choose a condition-controlled loop over a count-controlled one?
When the number of repetitions depends on something like user input or a value that changes as the loop runs, rather than a count you can write down first.

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 AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

How this lesson was checked. This AQA GCSE Computer Science (specification 8525)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 26 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.