7. Iteration
Docs / Pseudocode / Iteration
CAIE 0478 & 9618 Iteration

7. Iteration (Loops)

Count-controlled FOR..TO..STEP..NEXT, post-condition REPEAT..UNTIL, and pre-condition WHILE..ENDWHILE loops in Cambridge pseudocode.

Three standard loop constructs: count-controlled (FOR..NEXT), post-condition (REPEAT..UNTIL), and pre-condition (WHILE..ENDWHILE).

Standard Loop Constructs

PSEUDOCODE
// 1. FOR loop (count-controlled)
FOR i <- 1 TO 10
   OUTPUT "Index: ", i
NEXT i

// FOR with STEP increment (positive or negative)
FOR i <- 10 TO 0 STEP -2
   OUTPUT i
NEXT i

// 2. REPEAT .. UNTIL (post-condition)
// Executes at least once; terminates when condition evaluates to TRUE
REPEAT
   OUTPUT "Please enter the password"
   INPUT Password
UNTIL Password = "Secret"

// 3. WHILE .. ENDWHILE (pre-condition)
// Condition tested before each iteration; may execute zero times
WHILE Number > 9
   Number <- Number - 9
ENDWHILE

Syllabus Distinction: DO Keyword

Some IGCSE (0478) materials include a DO keyword after the WHILE condition (e.g. WHILE x < 5 DO). The AS & A Level (9618) specification omits DO — the condition is followed directly by the indented loop body. Both styles are valid and accepted in exams.

Nested FOR Loop Example (2D Accumulation)

PSEUDOCODE
Total <- 0
FOR Row <- 1 TO MaxRow
   RowTotal <- 0
   FOR Column <- 1 TO 10
      RowTotal <- RowTotal + Amount[Row, Column]
   NEXT Column
   OUTPUT "Total for Row ", Row, " is ", RowTotal
   Total <- Total + RowTotal
NEXT Row
OUTPUT "The grand total is ", Total