GCSE · Computer Science · AQA · Spec 8525

Iteration

Two loops. Same job. Same starting value. One runs its body zero times and the other runs it once, and the only difference is where the question gets asked.

Computer Science · Iteration

Same job, same start, different answer

Two loops that both try to charge a battery until it is full. The battery is already full. Step through and keep your eye on the highlighted line.

Before you press Next: how many times will each loop body run, 0 or 1?

1 # Version A: the question comes FIRST
2 battery ← 100
3 passes ← 0
4 WHILE battery < 100
5 battery ← battery + 20
6 passes ← passes + 1
7 ENDWHILE
8 OUTPUT passes
9 # Version B: the question comes LAST
10 battery ← 100
11 passes ← 0
12 REPEAT
13 battery ← battery + 20
14 passes ← passes + 1
15 UNTIL battery ≥ 100
16 OUTPUT passes

Variables

battery=100

Output

 

Step 1: Version A starts with a battery that is already full: 100.

1 / 11

Press Next and watch the highlighted line. Does it reach the loop's question before the body, or after it?

Step 1 of 11: Version A starts with a battery that is already full: 100..

Exam line: A loop with its condition at the start can run zero times. A loop with its condition at the end always runs at least once.
Watch out: The WHILE loop didn't break when it skipped its body. A start-checked loop is supposed to do nothing when its condition is false from the beginning.

Computer Science · Iteration

Which kind of loop is it?

Which kind of loop fits each one best?

Still to sort

Definite (count-controlled) (0)

Runs a set number of times, known before the loop starts.

Where the line is: If the number of passes depends on something that happens inside the loop, it is not definite.

Indefinite: condition at the start (0)

Repeats on a condition that is checked before the body.

Where the line is: Look at WHERE the condition is written, not which keyword you can see.

Indefinite: condition at the end (0)

Repeats on a condition that is checked after the body.

Where the line is: Look at WHERE the condition is written, not which keyword you can see.

7 of 7 still to sort.

You've just met two loops that repeat on a condition. There's also a loop that simply counts. Some of these are loop headers and some are jobs a program has to do: place each one, then read why.

UK note

AQA 8525: you need to understand loops with the condition at either end, even if the programming language you use only has one of them.

Computer Science · Iteration

UNTIL or WHILE?

A puzzle program contains this loop: REPEAT … make one move … UNTIL Solved

What does this loop do? Pick the idea closest to what you think right now.
How sure are you?

Computer Science · Algorithms

Trace table — dry run the code

A loop inside a loop: 2 rows of 3 seats. Will count finish on 5 or 6? Step through and watch seat each time a new row starts.

1count ← 0
2row ← 0
3WHILE row < 2
4 row ← row + 1
5 FOR seat ← 1 TO 3
6 count ← count + 1
7 ENDFOR
8ENDWHILE
9OUTPUT count
row
seat
count
Output
Press Start to run the first line.
·

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

Computer Science · Iteration

Put the pieces together

A teacher types in 5 test scores. A score of 40 or more is a pass. Complete the algorithm so it outputs how many of the 5 scores are passes.

  1. CONSTANT PASS_MARK ← 40Constant declaration: PASS_MARK is fixed and can't change while the program runs.
  2. passes ← 0Assignment: the counter starts at 0, before the loop.
  3. missing step
Which line is step 3?

Exam line: Sequence, selection and iteration are the three ways statements are combined in every high-level imperative language. Here they nest: an IF sits inside a FOR, and the lines before and after the loop each run once, in order.

Computer Science · Iteration

Your turn: write the loop

A cash machine asks for a PIN and keeps asking until the correct PIN, 4321, is entered. Write an algorithm that does this and then outputs how many attempts were needed. [4 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.

Count-controlled and condition-controlled loops, and why the place a condition sits changes what a program does

What you need to know

  • The difference between definite (count-controlled) and indefinite (condition-controlled) iteration, and how to choose between them.
  • What changes when a loop's condition is checked at the start rather than at the end.
  • How REPEAT … UNTIL and DO … WHILE stop, and why UNTIL is not the same as WHILE.
  • How to trace a nested loop, and how to write algorithms that combine loops with other statements.

The big picture

Iteration means repeating instructions with a loop. A count-controlled (definite) loop such as FOR i ← 1 TO 5 runs a number of times known before it starts, while a condition-controlled (indefinite) loop repeats until a condition says stop. Checked at the start (WHILE), the body might never run; checked at the end (REPEAT … UNTIL or DO … WHILE), it always runs at least once, and UNTIL stops when its condition becomes true. Loops can nest inside each other and combine with assignments, constants and IF statements, so every program is built from sequence, selection and iteration.

Key points

1Definite (count-controlled) iteration runs a fixed number of times, known before the loop starts: FOR i ← 1 TO 5 … ENDFOR runs 5 times.
2Indefinite (condition-controlled) iteration repeats until a condition says stop, so the number of passes isn't known in advance.
3Condition at the start (WHILE NotSolved … ENDWHILE): checked before the body, so the body can run zero times.
4Condition at the end (REPEAT … UNTIL Solved, or DO … WHILE NotSolved): checked after the body, so it always runs at least once.
5UNTIL stops the loop when its condition becomes true. WHILE keeps it going while its condition is true.
6In a nested loop the inner loop restarts and runs in full on every outer pass, so its body runs outer × inner times.
7Sequence, selection and iteration are the three ways statements are combined in all high-level imperative languages.

Worked example

Problem

Write an algorithm that asks for a whole number, keeps halving it (using DIV 2) until it gets down to 1, and outputs how many halvings that took. If the number typed is already 1, it should output 0.

⚠ Watch out

Reading UNTIL as if it meant WHILE. REPEAT … UNTIL Solved stops as soon as Solved becomes true. It does not keep going while Solved is true.

🧠

Memory hook

WHILE looks before it leaps. REPEAT leaps, then looks.

✓

Check yourself

Cover the page and answer out loud: when would you choose a FOR loop, a WHILE loop, and a REPEAT … UNTIL loop? Give one task that suits each.

Flashcards

(13)
What is iteration?
Repeating a set of instructions, using a loop.
What is definite (count-controlled) iteration?
A loop that runs a fixed number of times, known before it starts. Example: FOR i ← 1 TO 5 … ENDFOR runs 5 times.
What is indefinite (condition-controlled) iteration?
A loop that repeats until a condition says stop. The number of passes isn't known in advance.
FOR or a condition-controlled loop: what's the deciding question?
Do you know how many passes are needed before the loop starts? Yes: FOR. No: a condition-controlled loop.
WHILE NotSolved … ENDWHILE: where is the condition checked?
At the start, before the body. If the condition is false to begin with, the body runs zero times.
REPEAT … UNTIL Solved: where is the condition checked?
At the end, after the body. The body always runs at least once.
When does REPEAT … UNTIL Solved stop?
As soon as Solved is true. UNTIL means 'stop when this becomes true'.
Which loop does the same job as REPEAT … UNTIL Solved?
DO … WHILE NotSolved. Both check at the end, and both stop once the puzzle is solved.
DO … WHILE contains the word WHILE. Is it start-checked?
No. Its condition is on the last line, after the body. Where the condition sits decides the type, not the keyword.
When should the condition go at the end of a loop?
When the body has to happen at least once before there's anything to check, like asking for a password.
What is nested iteration?
A loop inside another loop.
In a nested loop, what happens to the inner loop on each pass of the outer loop?
It starts again from the beginning and runs all the way through, so the inner body runs outer × inner times.
What are the three ways statements are combined in high-level imperative programs?
Sequence, selection and iteration.

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