GCSE · Computer Science · AQA · Spec 8525

Indefinite (condition-controlled) iteration

How many times should a program ask for a password? Nobody knows until the user gets it right, so you need a loop that stops when a condition says so.

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 does not 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: store the first entry before the loop tests it. If it's already 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 depends on the data rather than being fixed before the loop starts.
  • With the condition at the start (WHILE … ENDWHILE), the check happens before every pass, so the body can run zero times.
  • With the condition at the end (REPEAT … UNTIL or DO … WHILE), the body runs first and is checked afterwards, so it always runs at least once.
  • UNTIL stops when its condition is TRUE; WHILE carries on while its condition is TRUE. REPEAT … UNTIL Solved does the same job as DO … WHILE NotSolved.
  • Loops can be nested: the inner loop runs right through, start to finish, on every pass of the outer loop. Loops also combine with constants, variables, assignment, selection and subroutines.

The big picture

A condition-controlled loop repeats until a condition tells it to stop, so the number of passes depends on the data rather than being fixed in advance. With the check at the start (WHILE … ENDWHILE) the body can run zero times; with the check at the end (REPEAT … UNTIL or DO … WHILE) it always runs at least once. UNTIL stops when its condition is true, while WHILE carries on while its condition is true. Loops can be nested, and they combine with constants, variables, assignment and selection to build real programs.

Key points

1A condition-controlled loop checks its condition on every pass, not just once.
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 new input or an updated variable. Otherwise a loop that starts may never stop.
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. If nothing can make the condition change as the loop runs, 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 starts at 5. How many times does WHILE count < 3 … ENDWHILE run its body? And how many times would REPEAT … UNTIL count ≥ 3 run it?

Flashcards

(13)
What is indefinite (condition-controlled) iteration?
A loop that keeps repeating until a condition tells it to stop. How many passes it makes depends on the data, not on a count fixed before the loop starts.
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, WHILE continues while its condition is TRUE, so the condition has to flip.
How often does a condition-controlled loop check its condition?
Once every pass: before each pass for a WHILE loop, after each pass for REPEAT … UNTIL or DO … WHILE.
Why might a condition-controlled loop never stop?
Nothing inside the body can change the condition, so it gives the same answer every time. The body needs something like new 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 every single pass of the outer loop.
In a nested loop, when is the outer condition checked?
Only when control gets back to the outer loop's condition, which is after the inner loop has finished its passes.
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 you can't know the number of repetitions before the loop starts, because it depends on something like user input or a value that changes as the loop runs.

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