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 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 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 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) | each iteration (bottom of loop) |
Two questions decide which loop fits a given task:
FOR. No → continue to question 2.REPEAT-UNTIL (the condition is checked at the bottom). No → use WHILE (the condition is checked at the top, and zero iterations are allowed).Two quick examples of the pattern in everyday programming. Printing the first twenty multiples of three has a known iteration count, so a FOR loop is the right pick. Asking the user to type a positive number and re-prompting if they type a negative one needs the input to be read at least once before any check happens, so REPEAT-UNTIL fits. Waiting for a sensor reading to drop below a threshold has unknown duration and may already be below at the start, so WHILE fits.
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.Converting a WHILE loop to a FOR loop
What comes up: describe the changes needed to rewrite a condition-controlled (WHILE) loop as a count-controlled (FOR) loop — a recurring "modify this algorithm" task.
Write: (1) replace the WHILE line with a FOR line that sets the counter's start and end limits; (2) remove the manual counter update from inside the loop body; (3) replace ENDWHILE with NEXT.
Watch out: each of those three changes is a separate mark — the one most often missed is leaving the manual counter update inside the loop by mistake.
| 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 |