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 ← 102 WHILE n < 503 OUTPUT n4 n ← n * 25 ENDWHILE6 OUTPUT 'Done'
Variables
Output
Step 1: n starts at 10. Nothing has been checked yet.
Press Next and watch where the highlight goes after each doubling. It always climbs back to the WHILE line.
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 · 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?
Ready when you are — step through one line at a time.
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
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?
Does 'indefinite' mean the loop runs forever?
WHILE … ENDWHILE: where is the condition, and what does that mean?
REPEAT … UNTIL: where is the condition, and when does it stop?
DO … WHILE: where is the condition, and when does it stop?
Which DO … WHILE loop does the same job as REPEAT … UNTIL Solved?
A WHILE loop runs its body 3 times. How many times was its condition checked?
Why might a condition-controlled loop never stop?
What is nested iteration?
A FOR loop is nested inside a WHILE loop. When is the WHILE condition checked again?
What are the three combining principles of high-level imperative programming?
Which other statement types can iteration be combined with in a program?
Do you need to understand checks at both ends of a loop if your language only has one kind?
When would you choose a condition-controlled loop over a count-controlled one?
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 AQA GCSE Computer Science topics
- Correcting errors
- Data validation routines
- Equal to (relational operator)
- Greater than (relational operator)
- Greater than or equal to (relational operator)
- Identifying and categorising errors
- Integer / real to string conversion
- Integer data type
- Integer division and remainder
- Iteration
- Keyboard input
- Less than (relational operator)
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.