Algorithm Design and Problem Solving · 4 question types
Past paper frequency (2018 to 2024)
This topic accounts for approximately 11% of your exam marks.
The most marks-dense topic. Both papers test pseudocode writing and trace tables every sitting.
CIE pseudocode supports three loop constructs. Each one fits a different situation; getting the right loop for the job makes algorithms cleaner and easier to read.
A FOR loop runs a block of code a fixed number of times, with a counter that takes each value in a stated range.
Syntax:
FOR i ← 1 TO 10
OUTPUT i
NEXT i
Use a FOR loop when:
Variations:
FOR i ← 1 TO 10 STEP 2 advances the counter by 2 each iteration (1, 3, 5, 7, 9).FOR i ← 10 TO 1 STEP -1 counts down.A WHILE loop runs a block of code as long as a condition is true. The condition is checked at the top before each iteration.
Syntax:
WHILE Answer <> "stop" DO
INPUT Answer
ENDWHILE
Use a WHILE loop when:
A REPEAT-UNTIL loop runs a block of code repeatedly until a condition becomes true. The condition is checked at the bottom after each iteration.
Syntax:
REPEAT
INPUT Number
UNTIL Number > 0
Use a REPEAT-UNTIL loop when:
| Feature | FOR | WHILE | REPEAT-UNTIL |
|---|---|---|---|
| Number of iterations | Known in advance | Not known | Not known |
| Condition checked | n/a (counter-controlled) | Before each iteration (top of loop) | After each iteration (bottom of loop) |
| Will it run at least once? | Only if the start ≤ end | No (zero iterations possible) | Yes (always at least one iteration) |
| Counter variable | Yes, the counter | Optional; the programmer manages it | Optional; the programmer manages it |
| Typical use | Traversing an array of known size; printing N lines; counting | Processing input until a sentinel value arrives; waiting for a condition | Validating user input; menu loops that should always run once |
Example — Which loop should you use in each scenario?
| Scenario | Best loop | Why |
|---|---|---|
| Print the numbers from 1 to 100 | FOR | Number of iterations is known: 100 |
| Sum every score in a class array of 30 students | FOR | Number of iterations is known: 30 |
| Keep asking the user for a positive number until they enter one | REPEAT-UNTIL | At least one input is needed; the loop continues until valid |
| Read lines from a file until the end-of-file marker | WHILE (WHILE NOT EOF) | May read zero lines if the file is empty |
| Display a menu and process choices until the user picks "Quit" | REPEAT-UNTIL | Menu must show at least once; loop ends on a specific choice |
| Search through unsorted data for a target value | WHILE (WHILE NOT found AND index < size) | May find the target on iteration 0, or never |
FOR i ← 1 TO 10 runs 10 times, not 9 or 11.WHILE Answer <> "stop" loop).REPEAT-UNTIL when zero iterations should be possible, or a WHILE when at least one iteration is mandatory.