# Iteration (Loops)

> Cambridge Assessment International Education (CAIE) Pseudocode Reference Guide
> Applicable Syllabus: **IGCSE &amp; A Level**

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 &lt;- 1 TO 10
   OUTPUT &quot;Index: &quot;, i
NEXT i

// FOR with STEP increment (positive or negative)
FOR i &lt;- 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 &quot;Please enter the password&quot;
   INPUT Password
UNTIL Password = &quot;Secret&quot;

// 3. WHILE .. ENDWHILE (pre-condition)
// Condition tested before each iteration; may execute zero times
WHILE Number &gt; 9
   Number &lt;- Number - 9
ENDWHILE
```

### Syllabus Distinction: DO Keyword

Some IGCSE (0478) materials include a `DO` keyword after the `WHILE` condition (e.g. `WHILE x &lt; 5 DO`). The AS &amp; 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 &lt;- 0
FOR Row &lt;- 1 TO MaxRow
   RowTotal &lt;- 0
   FOR Column &lt;- 1 TO 10
      RowTotal &lt;- RowTotal + Amount[Row, Column]
   NEXT Column
   OUTPUT &quot;Total for Row &quot;, Row, &quot; is &quot;, RowTotal
   Total &lt;- Total + RowTotal
NEXT Row
OUTPUT &quot;The grand total is &quot;, Total
```


---

* Previous Topic: [Selection (IF &amp; CASE OF)](https://csmasters.dev/docs/pseudocode/selection.md)
* Next Topic: [Procedures &amp; Functions](https://csmasters.dev/docs/pseudocode/subroutines.md)
