AS Level · Topic 11.2
A-Level Computer Science 9618 / Topic 11 / AS

Choose a path, then repeat.

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.

The key idea

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.

FOR (count)WHILE (pre)REPEAT (post) i ← 1i ≤ 10? bodyi ← i + 1 cond?body bodycond? TRUEFALSE TRUEFALSE FALSETRUE runs a fixed number of timesmay run 0 timesruns at least once
Fig. 1: where each loop tests its condition decides the fewest times its body can run
Section 01

IF, ELSE and nesting.

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
Worked example Dry-run the delivery charge
Give the output for (a) Weight 14.0, Express FALSE and (b) Weight 2.0, Express TRUE.
  1. (a) 14.0 <= 2.0 is FALSE, so the ELSE runs; 14.0 <= 10.0 is FALSE, so the inner ELSE runs.
  2. Charge ← 7.50 + (14.0 − 10.0) × 0.50 = 7.50 + 2.00 = 9.50. Express is FALSE, so no change.
  3. (b) 2.0 <= 2.0 is TRUE (it is a boundary value), so Charge ← 4.00.
  4. Express is TRUE, so Charge ← 4.00 × 2 = 8.00.
Answer: (a) Delivery charge: 9.50 (b) Delivery charge: 8.00
Your turn
What is output for Weight 6.0 and Express TRUE?
Examiner trap

Check boundary values. Changing <= to < moves a 2.0 kg parcel into the next price band, and examiners test exactly that value.

Section 02

The CASE structure.

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
Worked example Which clause runs?
For the grade CASE above, give Grade when Mark is 65, 49 and 104.
  1. 65: 80 TO 100 does not match; 65 TO 79 does, so Grade ← 'B' and the rest are skipped.
  2. 49: the first three ranges fail; 0 TO 49 matches, so Grade ← 'U'.
  3. 104: no range matches, so the OTHERWISE clause runs: Grade ← 'X'.
Answer: 'B', 'U' and 'X'
Your turn
A programmer adds the clause 60 TO 100 : Grade ← 'S' as the very first clause. Which grades can now never be given?
Examiner trap

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.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

What is the fewest number of times the body of a REPEAT ... UNTIL loop can run?

Quick check+10 XP

How many times does the body of FOR Count ← 2 TO 11 STEP 3 run?

Quick check+10 XP

How are the clauses of a CASE structure checked?

Quick check+10 XP

In the lab's WHILE loop program, why can the body be skipped completely?

Section 03

Three kinds of loop.

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
LoopBest whenJustification that earns the mark
FOR ... NEXTthe number of repetitions is known before the loop startse.g. processing every element of a 50-element array
REPEAT ... UNTILthe body must run at least oncee.g. validation: the data has to be input before it can be checked
WHILE ... ENDWHILEthe body might not need to run at alle.g. reading a file that may be empty, or a rogue value entered first
Worked example Trace a WHILE loop
Complete a trace table for the WHILE program above when 47 is input.
  1. Number = 47: 47 > 9 is TRUE, so Number ← 38.
  2. 38 > 9 TRUE, Number ← 29; 29 > 9 TRUE, Number ← 20.
  3. 20 > 9 TRUE, Number ← 11; 11 > 9 TRUE, Number ← 2.
  4. 2 > 9 is FALSE, so the loop ends after 5 passes and 2 is output (the remainder of 47 ÷ 9).
Answer:
NumberNumber > 9OUTPUT
47TRUE
38TRUE
29TRUE
20TRUE
11TRUE
2FALSE2
Your turn
What happens if 8 is input to the WHILE program, and why could a REPEAT loop not give the same result?
Examiner trap

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.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

A program reads every line of a text file, which might be empty. Which loop is most suitable?

Exam style+20 XP

X ← 12. IF X > 10 THEN IF X > 15 THEN OUTPUT "A" ELSE OUTPUT "B" ENDIF ELSE OUTPUT "C" ENDIF. What is output?

Exam style+20 XP

Which values are output by FOR i ← 1 TO 10 STEP 4 ... OUTPUT i ... NEXT i?

Exam style+20 XP

When does a REPEAT ... UNTIL Condition loop stop?

Skill unlocked

Selection and iteration constructs, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Nested IF statements, CASE with ranges and OTHERWISE, FOR loops with STEP, REPEAT and WHILE loops, tracing loops and justifying the choice of loop.