Selection and Iteration
Selection: IF...THEN...ELSE
Selection allows a program to choose between different execution paths based on whether a condition is TRUE or FALSE. Without selection, every program would execute the same steps every time, regardless of the data.
Single-branch selection — runs a block only when the condition is TRUE:
IF temperature > 30 THEN
OUTPUT "It is hot outside"
ENDIF
Two-branch selection — runs one of two blocks:
IF score ≥ 40 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
if score >= 40:
print("Pass")
else:
print("Fail")
The condition uses relational and Boolean operators: score ≥ 40 AND attempts < 3.
Every
IFmust end withENDIFin AQA pseudocode. TheELSEclause is optional — omit it when there is nothing to do if the condition is FALSE. ForgettingENDIFis one of the most penalised pseudocode errors in exam answers.
Worked example — Classify a temperature as cold, warm, or hot:
IF temp < 10 THEN
OUTPUT "Cold"
ELSE
IF temp < 25 THEN
OUTPUT "Warm"
ELSE
OUTPUT "Hot"
ENDIF
ENDIF
Nested Selection and Multi-way Branching
Nested selection places one IF statement inside another, handling more than two possible outcomes. Each inner IF is only reached when the outer condition has already been resolved.
IF score ≥ 70 THEN
OUTPUT "Distinction"
ELSE
IF score ≥ 55 THEN
OUTPUT "Merit"
ELSE
IF score ≥ 40 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDIF
ENDIF
Python uses elif to flatten this structure:
if score >= 70:
print("Distinction")
elif score >= 55:
print("Merit")
elif score >= 40:
print("Pass")
else:
print("Fail")
AQA pseudocode has no ELIF keyword — use nested IF...ELSE...ENDIF.
When to use nested selection vs separate IF statements: use nested selection when the conditions are mutually exclusive (only one can ever be true). Use separate IF statements when multiple conditions can all be true and each should be checked independently.
Worked example — A theme park allows entry if the visitor is over 18, OR over 12 with a guardian present:
IF age > 18 THEN
OUTPUT "Entry allowed"
ELSE
IF age > 12 AND hasGuardian = TRUE THEN
OUTPUT "Entry allowed with guardian"
ELSE
OUTPUT "Entry not permitted"
ENDIF
ENDIF
Definite Iteration: FOR Loops
Definite (count-controlled) iteration repeats a block of code a fixed, predetermined number of times. The loop variable is automatically incremented after each iteration.
FOR i ← 1 TO 5
OUTPUT i
NEXT i
Outputs: 1, 2, 3, 4, 5.
for i in range(1, 6):
print(i)
In AQA pseudocode,
FOR i ← 1 TO 5includes both endpoints — it runs for i = 1, 2, 3, 4, 5. Python'srange(1, 6)excludes the upper bound — it also produces 1 through 5, butrange(1, 5)would only produce 1 through 4.
Use a FOR loop when the number of repetitions is known before the loop starts.
Worked example — Output every multiple of 7 from 7 to 70:
FOR n ← 1 TO 10
OUTPUT n * 7
NEXT n
Worked example — Calculate the sum of integers from 1 to 100:
total ← 0
FOR i ← 1 TO 100
total ← total + i
NEXT i
OUTPUT total # 5050
The accumulator pattern (total ← total + i) appears very frequently in exam questions — initialise the accumulator to 0 before the loop, then add inside.
Indefinite Iteration: WHILE Loops
Indefinite (condition-controlled) iteration repeats a block as long as a condition remains TRUE. The condition is checked before each iteration — if it is FALSE from the start, the body never runs.
password ← ""
WHILE password ≠ "secret" DO
OUTPUT "Enter password: "
password ← USERINPUT
ENDWHILE
OUTPUT "Access granted"
password = ""
while password != "secret":
password = input("Enter password: ")
print("Access granted")
Use WHILE when the number of repetitions is unknown in advance — login attempts, reading until a sentinel value, game loops.
Initialise the loop variable before the WHILE. If
passwordis already"secret"before the loop starts, the body is skipped entirely — which may or may not be the intended behaviour. Always trace through the first iteration.
Worked example — Keep halving a number until it falls below 1, counting the steps:
number ← 100
steps ← 0
WHILE number ≥ 1 DO
number ← number / 2
steps ← steps + 1
ENDWHILE
OUTPUT steps # 7
A WHILE loop can execute zero times if the condition is false initially. This is a key difference from REPEAT...UNTIL.
Worth saving these ideas?
Turn what you've read into instant revision cards. Free to get started.
REPEAT...UNTIL: Condition at the End
REPEAT...UNTIL is the second form of indefinite iteration. The body executes first, and the condition is checked after each run. This guarantees the body runs at least once.
REPEAT
OUTPUT "Enter a number between 1 and 10: "
number ← USERINPUT
UNTIL number ≥ 1 AND number ≤ 10
OUTPUT "Valid input: " & STR(number)
The loop continues repeating while the condition is FALSE — it stops when the condition becomes TRUE. This is the opposite of WHILE.
Python has no native REPEAT...UNTIL — it is simulated with while True and break:
while True:
number = int(input("Enter a number between 1 and 10: "))
if 1 <= number <= 10:
break
WHILE vs REPEAT...UNTIL — exam comparison:
| Feature | WHILE | REPEAT...UNTIL |
|---|---|---|
| Condition checked | Before body runs | After body runs |
| Minimum executions | 0 | 1 (always runs at least once) |
| Loop continues while condition is… | TRUE | FALSE |
| Best use case | Body may not need to run at all | Body must always run at least once (e.g. input validation) |
Input validation is the classic REPEAT...UNTIL scenario — you always need to receive at least one input before you can check whether it is valid.
Nested Iteration and Trace Tables
Nested iteration places one loop inside another. The inner loop completes all its iterations for each single iteration of the outer loop.
Worked example — Print a 3×3 multiplication table:
FOR row ← 1 TO 3
FOR col ← 1 TO 3
OUTPUT row * col
NEXT col
NEXT row
Execution order: (1×1), (1×2), (1×3), then (2×1), (2×2), (2×3), then (3×1), (3×2), (3×3). The inner loop runs 3 complete passes for each of the 3 outer iterations — 9 outputs total.
Trace tables track variable values through each iteration. Exam questions frequently ask you to complete one — write every change, row by row.
row | col | row * col |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 2 | 2 |
| 1 | 3 | 3 |
| 2 | 1 | 2 |
| 2 | 2 | 4 |
| 2 | 3 | 6 |
| 3 | 1 | 3 |
| 3 | 2 | 6 |
| 3 | 3 | 9 |
In a trace table, record the value of every variable that changes in each row. Do not skip rows or merge iterations — each line corresponds to exactly one execution of the innermost statement being traced.
A nested loop over an n × m grid executes the body n × m times. Two nested loops each running 100 times produce 10,000 iterations of the inner body.
Common Exam Mistakes
1. Forgetting ENDIF or ENDWHILE
Every IF needs ENDIF; every WHILE needs ENDWHILE. Missing these in pseudocode answers loses marks even when the logic is otherwise correct. Count openings and closings when checking your answer.
2. Off-by-one errors in FOR loops
FOR i ← 0 TO 4 runs 5 times (0, 1, 2, 3, 4). When traversing a 5-element array (indices 0–4), use TO 4, not TO 5. Reading array[5] on a 5-element array is an out-of-bounds error.
3. Confusing WHILE and REPEAT...UNTIL loop termination direction
WHILE runs while the condition is TRUE and stops when it becomes FALSE. REPEAT...UNTIL runs until the condition is TRUE and stops when it becomes TRUE. Writing UNTIL password ≠ "secret" when you mean UNTIL password = "secret" produces the opposite behaviour.
4. Forgetting to initialise the accumulator before a FOR loop
# Wrong — total has no defined starting value
FOR i ← 1 TO 10
total ← total + i
NEXT i
Always write total ← 0 before the loop. An uninitialised variable holds an unpredictable value.
5. Choosing the wrong loop type
Use FOR when the number of repetitions is known before the loop starts. Use WHILE or REPEAT...UNTIL when the loop must continue until a condition changes.
Generate revision on any topic you study
Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set — personalised to your level.
Lessons on anything
Structured, level-matched lessons on any topic you study
Practice quizzes
Find out what you actually know before the exam does
Flashcard sets
Lock in key concepts with instant revision cards
Ask Aica
Stuck on something? Get a clear explanation, any time
Variables, Constants and Data Types
Arrays and Records
Related lessons
7 Slides
7 Slides
7 Slides