Selection lets a program choose which statements to run; iteration makes it repeat them. Write IF, nested IF and CASE exactly as the 9618 guide does, use all three loop types, and justify which loop suits a problem.
Selection uses IF ... THEN ... ELSE ... ENDIF (which can be nested) or CASE OF ... OTHERWISE ... ENDCASE, whose clauses are tested in order until one matches. Iteration has three forms: a count-controlled loop (FOR ... NEXT) repeats a known number of times; a post-condition loop (REPEAT ... UNTIL) tests at the end, so it always runs at least once; a pre-condition loop (WHILE ... ENDWHILE) tests at the start, so it may run zero times.
An IF statement chooses between two paths. When there are more than two outcomes, put another IF inside the ELSE: a nested IF. Every IF needs its own ENDIF, and the indentation should show which ELSE belongs to which IF.
DECLARE Weight : REAL
DECLARE Express : BOOLEAN
DECLARE Charge : REAL
INPUT Weight
INPUT Express
IF Weight <= 2.0 THEN
Charge ← 4.00
ELSE
IF Weight <= 10.0 THEN
Charge ← 7.50
ELSE
Charge ← 7.50 + (Weight - 10.0) * 0.50
ENDIF
ENDIF
IF Express = TRUE THEN
Charge ← Charge * 2
ENDIF
OUTPUT "Delivery charge: ", Charge
Check boundary values. Changing <= to < moves a 2.0 kg parcel into the next price band, and examiners test exactly that value.
When one variable is compared with several possible values or ranges, CASE is clearer than a chain of nested IFs. The clauses are tested in sequence; the first one that matches runs, and control then jumps to the statement after ENDCASE. OTHERWISE, if used, must be the last clause.
DECLARE Choice : CHAR
INPUT Choice
CASE OF Choice
'A' : OUTPUT "Add a record"
'E' : OUTPUT "Edit a record"
'D' : OUTPUT "Delete a record"
OTHERWISE : OUTPUT "Invalid choice"
ENDCASE
DECLARE Mark : INTEGER
DECLARE Grade : CHAR
INPUT Mark
CASE OF Mark
80 TO 100 : Grade ← 'A'
65 TO 79 : Grade ← 'B'
50 TO 64 : Grade ← 'C'
0 TO 49 : Grade ← 'U'
OTHERWISE : Grade ← 'X'
ENDCASE
Age > 17 AND Member = TRUE.60 TO 100 : Grade ← 'S' as the very first clause. Which grades can now never be given?CASE stops at the first match. Overlapping ranges are not an error, but every value is caught by the earlier range, so the later clause is dead code.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
What is the fewest number of times the body of a REPEAT ... UNTIL loop can run?
How many times does the body of FOR Count ← 2 TO 11 STEP 3 run?
How are the clauses of a CASE structure checked?
In the lab's WHILE loop program, why can the body be skipped completely?
Step through the loop programs in the lab (FOR, WHILE, REPEAT UNTIL and nested FOR with STEP) and watch the trace table fill. The lab lays out IF and WHILE in the IGCSE style; in a 9618 paper write THEN on the IF line and no DO after WHILE.
// count-controlled: the number of repetitions is known
FOR Count ← 10 TO 1 STEP -3
OUTPUT Count
NEXT Count
// outputs 10, 7, 4, 1
// post-condition: the body must run at least once
REPEAT
OUTPUT "Enter a mark from 0 to 100"
INPUT Mark
UNTIL Mark >= 0 AND Mark <= 100
// pre-condition: the body may not need to run at all
INPUT Number
WHILE Number > 9
Number ← Number - 9
ENDWHILE
OUTPUT Number
| Loop | Best when | Justification that earns the mark |
|---|---|---|
| FOR ... NEXT | the number of repetitions is known before the loop starts | e.g. processing every element of a 50-element array |
| REPEAT ... UNTIL | the body must run at least once | e.g. validation: the data has to be input before it can be checked |
| WHILE ... ENDWHILE | the body might not need to run at all | e.g. reading a file that may be empty, or a rogue value entered first |
| Number | Number > 9 | OUTPUT |
|---|---|---|
| 47 | TRUE | |
| 38 | TRUE | |
| 29 | TRUE | |
| 20 | TRUE | |
| 11 | TRUE | |
| 2 | FALSE | 2 |
The UNTIL condition is the condition to stop; the WHILE condition is the condition to keep going. Converting one loop into the other means reversing the condition.
Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.
A program reads every line of a text file, which might be empty. Which loop is most suitable?
X ← 12. IF X > 10 THEN IF X > 15 THEN OUTPUT "A" ELSE OUTPUT "B" ENDIF ELSE OUTPUT "C" ENDIF. What is output?
Which values are output by FOR i ← 1 TO 10 STEP 4 ... OUTPUT i ... NEXT i?
When does a REPEAT ... UNTIL Condition loop stop?
This skill is now recorded on your profile. Keep the chain going.