Docs

Pseudocode reference

Choosing a loop

When to use FOR, WHILE, or REPEAT UNTIL, how nested loops work, and how to make sure every loop ends.

updated November 2, 2026

Which loop to use

Choosing the wrong loop, or writing one that never stops, is a common way to lose marks. The difference is when the condition is checked.

LoopCondition checkedRuns at least once?Use it when
FORNever: it counts through a rangeYes, unless the range is emptyYou know how many times to repeat
WHILEBefore each passNoThe loop might not need to run at all
REPEAT ... UNTILAfter each passYesThe body must run once, such as asking for input

Count with FOR

Both bounds are included, so FOR Count <- 1 TO 5 runs five times. Add STEP to count in a different step, including backwards: FOR Count <- 10 TO 0 STEP -2.

Pseudocode
DECLARE Count : INTEGER
DECLARE Total : INTEGER
Total <- 0

FOR Count <- 1 TO 5
    Total <- Total + Count
NEXT Count

OUTPUT "Total = ", Total
Trace: Total after each pass
CountTotal
–0
11
23
36
410
515
Step 1 / 6

Repeat while a condition holds with WHILE

The condition is checked before every pass. If it is FALSE at the start, the body never runs. Something inside the body must change so the condition can become FALSE.

Pseudocode
DECLARE Number : INTEGER
INPUT Number

WHILE Number > 9 DO
    Number <- Number - 9
ENDWHILE

OUTPUT Number
Trace with input 28
NumberNumber > 9What happens
28TRUESubtract 9
19TRUESubtract 9
10TRUESubtract 9
1FALSEExit and output 1
Step 1 / 4

Validate input with REPEAT UNTIL

The body runs first and the condition is tested afterwards, so the user is always asked at least once. Write the UNTIL condition as the valid or finished state, not the invalid one.

If the first entry is wrong, the loop asks again. If it is right, the loop ends after one pass.

Pseudocode
DECLARE Password : STRING

REPEAT
    OUTPUT "Enter password"
    INPUT Password
UNTIL Password = "Secret"

Nested loops for tables and grids

Use one loop inside another to process rows and columns, such as a 2D array. The inner loop finishes all its passes for each pass of the outer loop. Reset per-row totals before the inner loop starts.

Pseudocode
DECLARE Row : INTEGER
DECLARE Column : INTEGER
DECLARE RowTotal : INTEGER
DECLARE GrandTotal : INTEGER
DECLARE Amount : ARRAY[1:3, 1:4] OF INTEGER

FOR Row <- 1 TO 3
    FOR Column <- 1 TO 4
        Amount[Row, Column] <- Row * Column
    NEXT Column
NEXT Row

GrandTotal <- 0
FOR Row <- 1 TO 3
    RowTotal <- 0
    FOR Column <- 1 TO 4
        RowTotal <- RowTotal + Amount[Row, Column]
    NEXT Column
    OUTPUT "Row ", Row, " total = ", RowTotal
    GrandTotal <- GrandTotal + RowTotal
NEXT Row

OUTPUT "Grand total = ", GrandTotal

Exam tip. Run it: the rows total 10, 20, and 30, and the grand total is 60.

Make sure every loop ends

Before you run a loop, check it against these questions.

  • Does each pass change something the condition depends on?
  • Can the condition eventually become FALSE for WHILE, or TRUE for UNTIL?
  • Are the bounds right, with no off-by-one mistakes?
  • Did you set counters and totals before the loop?

Watch out. If a program never finishes, it is usually stuck in a loop. Stop it, then trace the loop's condition through two or three passes.