SLIDE 1 / 10
CSZone.co.uk
Click anywhere to advance · Arrow keys also work
AQA 7517 · Paper 1 · 4.1.2b

Iteration
FOR / WHILE / DO UNTIL

AQA A-Level Computer Science · Section 4.1 Fundamentals of Programming

WHAT YOU'LL LEARN
FOR ENDFOR · WHILE ENDWHILE · DO UNTIL · When to use each · Trace tables
AQA SPEC LINK
4.1.2 — Iteration as a programming construct: count-controlled & condition-controlled
Count-Controlled

FOR...ENDFOR — Count-Controlled Loop

AQA SYNTAX
FOR i ← 1 TO 10
  OUTPUT i
ENDFOR

FOR i ← 0 TO LEN(array)-1
  OUTPUT array[i]
ENDFOR
Use FOR when you know exactly how many times to repeat. The loop variable increments automatically. Python equivalent: for i in range(1, 11):
Condition-Controlled

WHILE...ENDWHILE — Pre-Test Loop

AQA SYNTAX — INPUT VALIDATION
score ← INT(USERINPUT)
WHILE score < 0 OR score > 100
  OUTPUT "Invalid - enter 0 to 100"
  score ← INT(USERINPUT)
ENDWHILE
⚠️ WHILE checks condition before first iteration — if condition is False immediately, the body never executes (zero or more iterations).
Post-Test Loop

DO...UNTIL — Post-Test Loop

AQA SYNTAX
DO
  OUTPUT "Enter a positive number:"
  num ← INT(USERINPUT)
UNTIL num > 0
KEY DIFFERENCE
DO...UNTIL always runs the body at least once — condition checked after each iteration.
UNTIL vs WHILE
WHILE continues while condition is True. UNTIL continues until condition becomes True.
Comparison

Choosing the Right Loop

FOR — Count-Controlled
Use when number of iterations is known before the loop starts. E.g. print 1 to 10, process all items in an array.
WHILE — Pre-Test Condition-Controlled
Use when number of iterations is unknown and body may never execute. E.g. validation loops, waiting for events.
DO UNTIL — Post-Test Condition-Controlled
Use when body must run at least once. E.g. menus, first-prompt validation.
Nested Loops

Loops Inside Loops

MULTIPLICATION TABLE
FOR i ← 1 TO 5
  FOR j ← 1 TO 5
    OUTPUT STR(i) + " × " + STR(j) + " = " + STR(i*j)
  ENDFOR
ENDFOR
Nested loops run the inner loop fully for each iteration of the outer loop. 5×5 loops = 25 total executions of the inner body.
Trace Table

Tracing a WHILE Loop

total ← 0
count ← 1
WHILE count ≤ 4
  total ← total + count
  count ← count + 1
ENDWHILE
OUTPUT total
counttotalCondition (count≤4)
11True
23True
36True
410True
510False → EXIT
Output: 10 (sum of 1+2+3+4)
Infinite Loops

Avoiding Infinite Loops

❌ INFINITE LOOP
count ← 1
WHILE count > 0
  OUTPUT count
  count ← count + 1
ENDWHILE
count always > 0 — never exits!
✅ CORRECT
count ← 1
WHILE count ≤ 10
  OUTPUT count
  count ← count + 1
ENDWHILE
Exits when count = 11
AQA Exam Style

Practice Question

AQA 7517 — Paper 1 Style
Explain the difference between a FOR loop and a WHILE loop. Give one example of when you would use each.
[4 marks]
1 mark
FOR is count-controlled — the number of iterations is known/fixed before the loop starts
1 mark
Example: iterating through all elements of an array of known size
1 mark
WHILE is condition-controlled — repeats while a condition is True; iterations may be 0 or more
1 mark
Example: input validation — keep asking until valid input is provided
Summary

Key Points to Remember

FOR ENDFOR — count-controlled; use when iterations are known in advance
WHILE ENDWHILE — pre-test; condition checked first; 0 or more iterations
DO UNTIL — post-test; body runs at least once; condition checked after
Infinite loop = missing or broken termination condition — always trace!
Nested loops: inner loop runs completely for each outer iteration
🎉 Lesson complete — move to the quiz!