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.
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.
| Loop | Condition checked | Runs at least once? | Use it when |
|---|---|---|---|
FOR | Never: it counts through a range | Yes, unless the range is empty | You know how many times to repeat |
WHILE | Before each pass | No | The loop might not need to run at all |
REPEAT ... UNTIL | After each pass | Yes | The 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.
DECLARE Count : INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR Count <- 1 TO 5
Total <- Total + Count
NEXT Count
OUTPUT "Total = ", Total| Count | Total |
|---|---|
| – | 0 |
| 1 | 1 |
| 2 | 3 |
| 3 | 6 |
| 4 | 10 |
| 5 | 15 |
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.
DECLARE Number : INTEGER
INPUT Number
WHILE Number > 9 DO
Number <- Number - 9
ENDWHILE
OUTPUT Number| Number | Number > 9 | What happens |
|---|---|---|
| 28 | TRUE | Subtract 9 |
| 19 | TRUE | Subtract 9 |
| 10 | TRUE | Subtract 9 |
| 1 | FALSE | Exit and output 1 |
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.
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.
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 = ", GrandTotalExam 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
FALSEforWHILE, orTRUEforUNTIL? - 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.