📁 Paper 1 · 3.2 Programming
3.2.2c Iteration
AQA 8525 · GCSE Computer Science · ~12 min read
Notes
──
Video
──
Worksheet
──
Quiz

What is Iteration?

Iteration (repetition) allows a block of code to run multiple times without having to rewrite it. AQA specifies three loop types: FOR, WHILE, and REPEAT…UNTIL.

FOR Loop (Count-controlled)

Use when you know exactly how many times the loop should run.

FOR i ← 1 TO 5 OUTPUT i ENDFOR // Outputs: 1 2 3 4 5

The loop variable i automatically increments from the start value to the end value (inclusive). The default step is 1.

// Summing 1 to 10 total ← 0 FOR i ← 1 TO 10 total ← total + i ENDFOR OUTPUT total // 55

WHILE Loop (Pre-condition)

Use when the number of repetitions is not known in advance. The condition is checked before each iteration — so the body may never run at all if the condition starts False.

count1 WHILE count <= 5 OUTPUT count countcount + 1 ENDWHILE // Outputs: 1 2 3 4 5

Input validation is a classic WHILE use case — keep asking until valid input is received:

age ← int(INPUT("Enter age: ")) WHILE age < 0 OR age > 120 OUTPUT "Invalid — try again" age ← int(INPUT("Enter age: ")) ENDWHILE

REPEAT…UNTIL Loop (Post-condition)

The condition is checked after each iteration — so the body always runs at least once.

REPEAT passwordINPUT("Enter password: ") UNTIL password == "secret123"

Keeps asking until the password is correct. Perfect when you want the user to enter data at least once before checking validity.

Comparison of the Three Loops

LoopWhen to useCondition checkedMin runs
FORKnow exact countStart/end of count0 (if start > end)
WHILEDon't know count; check firstBefore each iteration0
REPEAT…UNTILDon't know count; run firstAfter each iteration1

Infinite Loops

A loop with a condition that is always True (or never becomes False) will run forever — this is a bug. Common cause: forgetting to update the loop variable inside a WHILE loop.

// INFINITE LOOP — bug! count ← 1 WHILE count <= 5 OUTPUT count // Missing: count ← count + 1 ENDWHILE
Exam tip: REPEAT…UNTIL loops once guaranteed — the condition is at the bottom. WHILE may run zero times — the condition is at the top. Exam questions often ask you to pick the correct loop type for a scenario or trace through one.
⚠️ Common Mistakes
  • Using ENDFOR after a WHILE loop (and vice versa) — every loop has its own closing keyword
  • Forgetting to update the counter in a WHILE loop → infinite loop
  • FOR i ← 1 TO 5 runs 5 times (1,2,3,4,5) — remember the end value is inclusive
  • UNTIL condition is when to STOP (opposite of WHILE condition which is when to CONTINUE)
Video coming soon

Key points

  • FOR loop — count-controlled, inclusive range
  • WHILE loop — pre-condition, may run 0 times
  • REPEAT…UNTIL — post-condition, always runs at least once
  • Input validation using WHILE
Click slide or press arrow keys to navigate
✍️

Worksheet — 3.2.2c Iteration

8 questions · 24 marks

Q1State the three types of iteration in AQA pseudo-code and state when each is used.[6]
✅ Mark scheme
Mark scheme
FOR — when exact number of iterations known [2]; WHILE — condition checked before, used when count unknown/may run 0 times [2]; REPEAT…UNTIL — condition checked after, always runs at least once [2].
Q2Trace this loop and state all output: FOR i ← 1 TO 5, OUTPUT i * i, ENDFOR[2]
✅ Mark scheme
Mark scheme
1 4 9 16 25 [2] — one mark if 4 correct.
Q3Write a WHILE loop that keeps asking the user for a number until they enter a number greater than 0.[4]
✅ Mark scheme
Mark scheme
Initial INPUT before loop [1]; WHILE num <= 0 [1]; OUTPUT error message [1]; INPUT inside loop [1]; ENDWHILE [implicit in structure].
Q4What is the key difference between a WHILE loop and a REPEAT…UNTIL loop?[2]
✅ Mark scheme
Mark scheme
WHILE checks condition before each iteration — may run 0 times [1]; REPEAT…UNTIL checks condition after each iteration — always runs at least once [1].
Q5Write pseudo-code using a FOR loop to output all even numbers between 2 and 20 inclusive.[3]
✅ Mark scheme
Mark scheme
FOR i ← 2 TO 20 (or 1 TO 10) [1]; IF i MOD 2 == 0 THEN OUTPUT i (or OUTPUT i*2 if using 1-10) [1]; ENDFOR [1]. Accept step-2 approach if known.
Q6Identify the bug in this code and explain the consequence: count ← 1, WHILE count <= 10, OUTPUT count, ENDWHILE[2]
✅ Mark scheme
Mark scheme
count is never incremented [1]; this creates an infinite loop that outputs 1 forever [1].
Q7How many times does this loop run: FOR i ← 5 TO 1, OUTPUT i, ENDFOR? Explain your answer.[2]
✅ Mark scheme
Mark scheme
0 times [1]; the start (5) is already greater than the end (1), so the loop does not execute [1].
Q8Write a REPEAT…UNTIL loop that asks the user to guess a number until they guess 7.[3]
✅ Mark scheme
Mark scheme
REPEAT [1]; guess ← int(INPUT("Guess: ")) [1]; UNTIL guess == 7 [1].
Check your answers above.
Topic Quiz
Q 1 of 10
You scored
out of 10
Card 1 of 6
Click to flip
🎉
All done!
TermDefinition
🎯

Mini Test — 3.2.2c Iteration

Timed exam conditions.

  • 8 questions · 10 minutes
  • 5 MCQ + 3 short answer
← 3.2.2b Selection
12 of 57 · AQA 8525
3.2.2d Arrays →