IF … THEN … ELSE … END IF and CASE … OF … END CASEFOR … TO … STEP … DO … END FOR, WHILE … DO … END WHILE, REPEAT … UNTIL … END REPEAT| Construct | Purpose | Pseudocode Syntax (Cambridge style) |
|---|---|---|
| IF‑statement | Execute a block only when a condition is true; optionally provide an alternative block. | IF condition THEN |
| CASE (SWITCH) statement | Select one of many alternatives based on the value of an expression. | CASE expression OF |
| FOR‑loop (count‑controlled) | Repeat a known number of times; the loop counter is initialised, tested and updated automatically. | FOR i ← start TO end [STEP step] DO |
| WHILE‑loop (pre‑condition) | Repeat while a condition is true; the condition is tested before the first iteration. | WHILE condition DO |
| REPEAT‑UNTIL loop (post‑condition) | Execute the body at least once and repeat until a condition becomes true; the condition is tested after each iteration. | REPEAT |
Used when a single condition determines which block of code runs.
DECLARE mark : INTEGER
INPUT "Enter the mark (0‑100): ", mark
IF mark ≥ 70 THEN
OUTPUT "Grade: A"
ELSE IF mark ≥ 60 THEN
OUTPUT "Grade: B"
ELSE IF mark ≥ 50 THEN
OUTPUT "Grade: C"
ELSE
OUTPUT "Grade: F"
END IF
Often required when two or more conditions must be checked sequentially.
DECLARE age : INTEGER
DECLARE licence : CHARACTER
INPUT "Enter age: ", age
INPUT "Do you have a driving licence? (Y/N): ", licence
IF age ≥ 18 THEN
IF licence = 'Y' THEN
OUTPUT "You may drive."
ELSE
OUTPUT "You need a licence."
END IF
ELSE
OUTPUT "You are too young to drive."
END IF
Best when a variable can take one of several discrete values. The OTHERWISE clause is optional.
DECLARE grade : CHARACTER
INPUT "Enter grade (A‑F): ", grade
CASE grade OF
'A': OUTPUT "Excellent"
'B': OUTPUT "Very good"
'C': OUTPUT "Good"
'D': OUTPUT "Satisfactory"
'F': OUTPUT "Fail"
OTHERWISE: OUTPUT "Invalid grade"
END CASE
FORUse when the exact number of iterations is known before the loop starts.
FOR i ← start TO end [STEP step] DO
// loop body
END FOR
STEP -1 for a decrementing loop.DECLARE sum, i : INTEGER
sum ← 0
FOR i ← 1 TO 10 DO
sum ← sum + i
END FOR
OUTPUT "The sum is ", sum
DECLARE sum, i : INTEGER
sum ← 0
FOR i ← 2 TO 20 STEP 2 DO
sum ← sum + i
END FOR
OUTPUT "Sum of evens = ", sum
FOR i ← 10 TO 1 STEP -1 DO
OUTPUT i
END FOR
Some past papers use the phrase “count‑down” and write FOR i DOWNTO 1 DO. The Cambridge syntax is always FOR i ← … TO … STEP …; simply use a negative STEP to achieve the same effect.
Inclusive bound – FOR i ← 1 TO 5 runs with i = 1,2,3,4,5 (5 iterations).
Exclusive bound – If the requirement is “repeat *until* 5”, the correct loop is FOR i ← 1 TO 4. Adjust the end value or use a WHILE loop where the condition is i < 5.
WHILEUse when the number of repetitions is not known in advance; the condition is checked before each iteration.
DECLARE total, number : INTEGER
total ← 0
INPUT "Enter a number (0 to stop): ", number
WHILE number ≠ 0 DO
total ← total + number
INPUT "Enter a number (0 to stop): ", number
END WHILE
OUTPUT "Total = ", total
DECLARE n : INTEGER
INPUT "Enter a positive integer: ", n
WHILE n < 0 DO // condition false initially → body skipped
OUTPUT "This will never be printed."
n ← n + 1
END WHILE
OUTPUT "Program continues."
REPEAT … UNTILGuarantees that the body executes at least once; the condition is tested after the iteration.
DECLARE password : STRING
REPEAT
INPUT "Enter password (min 6 chars): ", password
UNTIL LENGTH(password) ≥ 6
OUTPUT "Password accepted"
DECLARE n : INTEGER
REPEAT
INPUT "Enter a number (1‑100): ", n
UNTIL n ≥ 1 AND n ≤ 100
OUTPUT "You entered ", n
| Situation | Preferred Loop | Justification (AO2/AO3) |
|---|---|---|
| Exact number of repetitions known beforehand | FOR | Counter is managed automatically; the algorithm’s time‑complexity is obvious (O(n)). Ideal for AO2 where the candidate must “design, write and trace” a solution. |
| Number of repetitions depends on input or a condition that may be false initially | WHILE | Condition is evaluated before the first iteration, so the loop may execute zero times – a common trick in exam questions testing understanding of pre‑conditions (AO2). The loop’s termination condition must be justified to avoid infinite loops (AO3). |
| Loop must run at least once (e.g., menu display, password entry) | REPEAT … UNTIL | Post‑condition guarantees one execution, matching specifications that require an initial prompt. Demonstrates control‑flow analysis (AO3) and clear algorithmic design. |
| Simple selection among many discrete options | CASE | More readable than nested IFs; reduces cyclomatic complexity, which is a point of interest for AO3 (analysis of algorithm efficiency and readability). |
FOR is chosen because the number of rows (12) is known in advance. The loop clearly shows a linear O(12) time‑complexity and the counter i maps directly to the multiplier.WHILE is appropriate because the number of entries cannot be predicted; the loop must stop only when the sentinel value -1 appears. The pre‑condition ensures the body may be skipped if the first input is the sentinel, satisfying the “zero‑iteration” case.REPEAT … UNTIL guarantees the user is prompted at least once, which is required for a password entry routine. The termination condition combines two Boolean checks, illustrating compound conditions and post‑condition logic.WHILE and REPEAT).< instead of ≤ (or vice‑versa) causes off‑by‑one errors.STEP with a decreasing range (or negative step with an increasing range) makes the loop never terminate.FOR i ← a TO b includes b. Adjust b if the specification says “up to but not including”.FOR when the number of iterations depends on user input; examiners award marks for recognising the most suitable construct.Multiplication table for 7 (use a count‑controlled loop)
Justification: The table always has 12 rows, so a FOR loop cleanly expresses the known iteration count.
DECLARE i, product : INTEGER
FOR i ← 1 TO 12 DO
product ← 7 * i
OUTPUT 7, " × ", i, " = ", product
END FOR
Sentinel‑controlled input: read integers until the user enters -1, then output the average
Justification: The number of values is unknown; a WHILE loop stops only when the sentinel appears, handling the possible zero‑input case.
DECLARE sum, count, n : INTEGER
sum ← 0
count ← 0
INPUT "Enter a number (-1 to stop): ", n
WHILE n ≠ -1 DO
sum ← sum + n
count ← count + 1
INPUT "Enter a number (-1 to stop): ", n
END WHILE
IF count = 0 THEN
OUTPUT "No numbers entered."
ELSE
OUTPUT "Average = ", sum / count
END IF
Password validator: prompt for a password until it contains at least one digit and one uppercase letter (use REPEAT‑UNTIL)
Justification: The user must be asked at least once; the loop terminates only when both conditions are satisfied, making REPEAT … UNTIL the natural choice.
DECLARE pwd : STRING
FUNCTION hasDigit(s) RETURNS BOOLEAN
RETURN (s CONTAINS "0" OR s CONTAINS "1" OR s CONTAINS "2"
OR s CONTAINS "3" OR s CONTAINS "4" OR s CONTAINS "5"
OR s CONTAINS "6" OR s CONTAINS "7" OR s CONTAINS "8"
OR s CONTAINS "9")
END FUNCTION
FUNCTION hasUpper(s) RETURNS BOOLEAN
RETURN (s CONTAINS "A" OR s CONTAINS "B" OR s CONTAINS "C"
OR s CONTAINS "D" OR s CONTAINS "E" OR s CONTAINS "F"
OR s CONTAINS "G" OR s CONTAINS "H" OR s CONTAINS "I"
OR s CONTAINS "J" OR s CONTAINS "K" OR s CONTAINS "L"
OR s CONTAINS "M" OR s CONTAINS "N" OR s CONTAINS "O"
OR s CONTAINS "P" OR s CONTAINS "Q" OR s CONTAINS "R"
OR s CONTAINS "S" OR s CONTAINS "T" OR s CONTAINS "U"
OR s CONTAINS "V" OR s CONTAINS "W" OR s CONTAINS "X"
OR s CONTAINS "Y" OR s CONTAINS "Z")
END FUNCTION
REPEAT
INPUT "Enter password: ", pwd
UNTIL hasDigit(pwd) AND hasUpper(pwd)
OUTPUT "Password accepted"
IF, CASE) direct program flow based on conditions.FOR, WHILE, REPEAT‑UNTIL) implement loops; choose the one that matches the problem’s known/unknown iteration count and required first‑/last‑iteration behaviour.STEP for non‑unit increments or decrements, and be mindful of inclusive vs. exclusive bounds.Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.