Programs make decisions with selection and repeat work with iteration. Master the two selection constructs (IF and CASE), the three loop types (FOR, WHILE, REPEAT), the counter and totaller patterns, string manipulation, and how they combine when nested. This is where a script becomes a real program.
Read this before you tap the activities. These notes give you everything you need before the Control Flow Simulator. When you're ready, tap 🎓 Learn.
You've learned that programs run in sequence — top to bottom. Now they need to decide and repeat. Selection chooses which lines to run based on data (IF for two-way, CASE for value-matching). Iteration runs lines repeatedly using one of three loops: FOR (count-controlled), WHILE (pre-condition), REPEAT (post-condition). Choosing the RIGHT loop for the task is the biggest single skill in this section. Add string handling and nesting and you can express any real-world logic in code.
By the end of Topic 8.6–8.11 you should be able to…
| Term | One-line definition |
|---|---|
| Selection | A control structure that chooses which code to run based on a condition. |
| IF statement | Two-way selection: IF cond THEN ... ELSE ... ENDIF. |
| CASE statement | Multi-way selection based on the value of a single variable. |
| OTHERWISE | The default branch of a CASE — runs when no other case matches. |
| Iteration | A control structure that repeats code — also called looping. |
| FOR loop | Count-controlled loop: runs a fixed number of times. |
| WHILE loop | Pre-condition loop: checks the condition BEFORE running the body. May run 0 times. |
| REPEAT loop | Post-condition loop: runs the body FIRST, checks after. Always runs ≥ 1 time. |
| Counter | Variable that counts occurrences. Starts at 0, adds 1 when condition met. |
| Totaller | Variable that accumulates values (also called accumulator). Starts at 0, adds values. |
| Nested loop | A loop inside another loop. |
| SUBSTRING | Function that extracts a portion of a string: SUBSTRING(str, start, length). |
| LENGTH | Function that returns the count of characters in a string. |
| LCASE / UCASE | Functions that return a string in lower / upper case. |
| Use | When to use it | Example |
|---|---|---|
| IF | Two-way (or few branches) based on any condition — including >, <, ranges, combined logic. | IF Score >= 50 THEN Pass ELSE Fail |
| CASE | Multiple branches based on a SINGLE variable's value — cleaner than long IF chains. | CASE Grade OF 'A': ... 'B': ... OTHERWISE ... |
| Loop | When to use it | Runs at least once? |
|---|---|---|
| FOR | You know EXACTLY how many iterations — a fixed number. | Yes (if range valid) |
| WHILE | You DON'T know how many — you check a condition first. Loop may run 0 times. | ❌ No — condition might be false immediately |
| REPEAT | You DON'T know how many, but the body must run AT LEAST ONCE — condition checked after. | ✅ Yes — always at least once |
| Pattern | Purpose | Update line |
|---|---|---|
| Counter | Count how many items match a condition | Count <- Count + 1 |
| Totaller | Sum values into a running total | Total <- Total + Value |
| Function | Returns | Example |
|---|---|---|
| LENGTH(s) | INTEGER count of characters | LENGTH("hello") = 5 |
| SUBSTRING(s, start, len) | STRING extract, start is 1-based | SUBSTRING("HELLO", 2, 3) = "ELL" |
| LCASE(s) | STRING in lower case | LCASE("Hello") = "hello" |
| UCASE(s) | STRING in upper case | UCASE("Hello") = "HELLO" |
Keep asking for an age until the user enters a valid one (0–120):
DECLARE Age : INTEGER
INPUT Age
WHILE Age < 0 OR Age > 120
OUTPUT "Invalid age. Try again."
INPUT Age
ENDWHILE
OUTPUT "Age accepted: ", Age
WHILE is right here because the user MIGHT enter a valid age first time (loop runs 0 times). If we used REPEAT, we'd ask twice minimum.
Print the first 3 rows of a multiplication table:
FOR i <- 1 TO 3
FOR j <- 1 TO 5
OUTPUT i * j, " "
NEXT j
OUTPUT NEWLINE
NEXT i
Outer FOR = rows. Inner FOR = columns. Each iteration of the outer loop makes the inner loop run its full range. Close inner NEXT before outer NEXT.
Get the first character of a name as a single character:
DECLARE Name : STRING DECLARE Initial : CHAR INPUT Name Initial <- SUBSTRING(Name, 1, 1) OUTPUT "Initial: ", Initial
Cambridge SUBSTRING is 1-based (start=1 for the first character). Length=1 gives you exactly one character.
FOR is for a FIXED number of iterations. If you're waiting for user input to satisfy a condition, you need WHILE or REPEAT. Cambridge examiners have flagged "attempted to rewrite the algorithm with another FOR loop" as a recurring error.
Cambridge CASE statements need an OTHERWISE (the default). Without it, an unmatched value has no defined behaviour. It's not optional in Cambridge pseudocode.
WHILE checks BEFORE (may run 0 times). REPEAT checks AFTER (always ≥ 1 time). Ask yourself: "Should the body run even if the condition is false initially?" If yes → REPEAT. If no → WHILE.
Cambridge SUBSTRING is 1-based. SUBSTRING("HELLO", 1, 2) returns "HE", not "EL". Some languages are 0-based; Cambridge is not.
Every FOR needs a matching NEXT with the same variable name. Nested loops need TWO NEXTs — inner one first, then outer. "closing the nested loops with NEXT statements" (2025 w25 examiner report) — students forget the inner NEXT.
LENGTH("hello") returns 5 — the count. That's the SAME as the last valid position (because Cambridge is 1-based). Careful when using LENGTH in loop ranges — FOR i <- 1 TO LENGTH(s) visits every character.
Mark-winning tips from the 2023–2025 examiner reports:
Tap each question to reveal the answer. Aim for at least 5 of 6.
SUBSTRING("PROGRAM", 3, 4)?LENGTH("Cambridge") return?Name the three loops and pick the right one. Write CASE with OTHERWISE. Trace a nested loop. Use SUBSTRING with the correct 1-based start.
Ready? Tap the 🎓 Learn tab and try the Control Flow Simulator — step through five constructs and see them run live.
Selection lets a program run different code depending on the data. IF handles two-way decisions and can chain into multi-way. CASE is cleaner when you're switching on a SINGLE variable's value. Iteration lets a program repeat code. Three loops exist: FOR (count-controlled), WHILE (checks before), REPEAT (checks after). Picking the right one is the difference between clean code and a bug hunt. Add the counter and totaller patterns, string manipulation with LENGTH and SUBSTRING, and the ability to nest loops inside each other — and you've got the toolkit for every Paper 2 algorithmic question.
Draw three empty boxes labelled FOR, WHILE, REPEAT. For each of these tasks, write which loop you'd use: (1) print numbers 1 to 10; (2) keep asking for a password until it's correct; (3) roll a die until you get a 6; (4) sum an array of 20 known scores. Answers: FOR, REPEAT, REPEAT, FOR. Notice how the answer depends on whether you know the iteration count and whether the body must run at least once.
Every real language has these three loop types (or equivalents). Python has for, while, and no direct REPEAT (you fake it with while True and break). Java, C, and JavaScript have for, while, and do...while (their REPEAT). The choice you make in Cambridge pseudocode maps directly to what you'd write on the job — the skill isn't about pseudocode, it's about picking the right control structure.
If REPEAT always runs at least once, why does WHILE exist? What kind of task specifically needs a loop that might not run at all? Give an example of code where using REPEAT instead of WHILE would cause a bug.
The single most tested skill in this section. Ask two questions: (1) Do I know the exact count? (2) Must the body run at least once?
Use when: You know EXACTLY how many iterations.
Runs 0 times? Only if range is empty.
Syntax:
FOR i <- 1 TO 10
OUTPUT i
NEXT i
Example use: print numbers 1–10; process an array of known size.
Use when: You don't know the count AND the body might not run at all.
Runs 0 times? Yes — if condition is false first.
Syntax:
WHILE Age < 18
INPUT Age
ENDWHILE
Example use: input validation (may already be valid); reading a file until EOF.
Use when: You don't know the count BUT the body must run at least once.
Runs 0 times? ❌ Never — always ≥ 1.
Syntax:
REPEAT
INPUT Password
UNTIL Password = "abc"
Example use: ask for input then check; menu that always shows once.
Pick a construct. Click Step. Watch the highlighted line run, state update, output print. Mode 4 (WHILE vs REPEAT) runs both loops side-by-side on the same task so you can see the pre- vs post-condition difference in one click.
No variables yet. Click Step to begin.
The exact syntax examiners expect.
"Some candidates incorrectly attempted to rewrite the algorithm with another FOR loop." — 2025 s25 examiner report. When the task needs a condition-controlled loop (validation, keep-asking-until, unknown iterations), FOR is wrong. Use WHILE or REPEAT.
Cited: 2025 s25 examiner report — verbatim"closing the nested loops with NEXT statements" — 2025 w25 examiner report. Every FOR needs its matching NEXT with the same variable. Nested loops have TWO NEXTs — inner one first, then outer. Skipping either loses marks.
Cited: 2025 w25 examiner report — verbatim"Some candidates incorrectly answered the question using pseudocode only. The question required an explanation." — 2025 s25 examiner report. If the question asks you to explain, write words. Pseudocode alone doesn't score explanation marks.
Cited: 2025 s25 examiner report — verbatim"A common error amongst nearly correct solutions was not providing enough evidence to show all the iterations of the algorithm through to its full completion." — 2024 s24 examiner report. Trace every iteration. Don't summarise.
Cited: 2024 s24 examiner report — verbatimCambridge CASE statements require an OTHERWISE branch. It's the default that fires when no case matches. Leaving it out is a syntax error in Cambridge pseudocode.
Cited: syllabus requirement (0478)Cambridge SUBSTRING is 1-based. SUBSTRING("HELLO", 1, 2) = "HE". Some students carry over 0-based habits from Python/Java — that's wrong here.
A task is described. Pick the RIGHT loop: FOR, WHILE, or REPEAT.
Given an IF/ELSEIF chain and a variable value, which branch runs?
Given nested loops, how many times does the inner body run in total?
Given a SUBSTRING call, what's the result?
10 rapid questions on loops, selection, strings, nesting. Beat your best.
Every question below is drawn from a real Cambridge Paper 2 (2022–2025) or the examiner report describing it. Type your answer, then reveal the model.
When the task needs condition-based iteration (validation, keep-asking-until), students force a FOR. Use WHILE or REPEAT.
2025 s25 examiner report — verbatimEvery FOR needs its matching NEXT (same variable). Nested loops need TWO NEXTs — inner first, then outer.
2025 w25 examiner report — verbatimQuestion asks to explain? Write words. Pseudocode alone doesn't score explanation marks.
2025 s25 examiner report — verbatim"Not providing enough evidence to show all the iterations of the algorithm through to its full completion." — 2024 s24. Every iteration.
2024 s24 examiner report — verbatimCambridge CASE requires OTHERWISE. It's the default. Without it, unmatched values have no defined behaviour.
0478 syllabus requirementCambridge SUBSTRING is 1-based. Position 1 = first character. Not 0-based like Python.
2025 s25 SUBSTRING questionIf the WHILE condition never becomes false, the loop runs forever. Make sure the body changes something that eventually makes the condition false.
Common Paper 2 mistakeIn IF-ELSEIF chains, only the FIRST matching branch runs. Order the conditions from most specific to most general (e.g. Score >= 80 before Score >= 50).
Common Paper 2 mistake"WHILE X AND Y" continues only while BOTH are true. "WHILE X OR Y" continues while EITHER is true. Wrong operator = wrong exit behaviour.
Recurring across 2023–2025The iteration types.
WHILE (pre-condition, 0+ runs) · REPEAT (post-condition, 1+ runs) · FOR (count-controlled).
Loop choice by check timing.
Pre-condition → WHILE (check first). Post-condition → REPEAT (run then check). Fixed count → FOR.
CASE always has a default.
Miss OTHERWISE = miss marks. Every CASE OF gets OTHERWISE before ENDCASE.
1-based, length is a count. SUBSTRING("HELLO", 1, 2) = "HE". Start=1 for first character. Length=count, not end position.
The two accumulator patterns.
Counter: Count <- Count + 1 when condition met.
Totaller: Total <- Total + Value every iteration.
Green = secured (≥70%). Yellow = focus (30–69%). Grey = not attempted. Tap any badge to override.