Modula-2 loops
last modified November 26, 2026 Modula-2
A loop runs a group of statements several times. Modula-2 has four loop
statements: WHILE tests its condition before each pass,
REPEAT after each pass, FOR counts through a
range of values, and LOOP runs until an EXIT
statement leaves it. Like the conditionals, every loop is closed with a
keyword, so its body may hold any number of statements.
WHILE loops
WHILE condition DO statements END runs the statements as long
as the condition is TRUE. The condition is tested before every
pass, so the body may not run at all.
MODULE doubling;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
VAR
grains, square: CARDINAL;
BEGIN
grains := 1;
square := 1;
WHILE grains < 1000 DO
grains := grains * 2;
square := square + 1
END;
WriteString("Square ");
WriteCard(square, 0);
WriteString(" is the first with 1000 grains or more: ");
WriteCard(grains, 0);
WriteLn
END doubling.
The number of grains doubles on every square of a chessboard. The loop stops as soon as the count reaches 1000; the body must change something the condition depends on, or the loop never ends.
$ gm2 doubling.mod -o doubling $ ./doubling Square 11 is the first with 1000 grains or more: 1024
REPEAT loops
REPEAT statements UNTIL condition tests its condition after
each pass, so the body always runs at least once. The loop stops when the
condition becomes TRUE.
MODULE digits;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
VAR
n, rest, count, sum: CARDINAL;
BEGIN
n := 90417;
rest := n;
count := 0;
sum := 0;
REPEAT
sum := sum + rest MOD 10;
rest := rest DIV 10;
count := count + 1
UNTIL rest = 0;
WriteCard(n, 0);
WriteString(" has ");
WriteCard(count, 0);
WriteString(" digits, their sum is ");
WriteCard(sum, 0);
WriteLn
END digits.
Each pass takes the last digit with MOD 10 and removes it
with DIV 10. A REPEAT loop fits here because
every number, even 0, has at least one digit. UNTIL ends the
loop, so no END is needed.
$ gm2 digits.mod -o digits $ ./digits 90417 has 5 digits, their sum is 21
FOR loops
FOR i := first TO last DO statements END assigns each value
from first to last to the control variable in
turn and runs the body once for each.
MODULE squares;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
VAR
i: CARDINAL;
BEGIN
WriteString(" n n*n n*n*n");
WriteLn;
FOR i := 1 TO 6 DO
WriteCard(i, 3);
WriteCard(i * i, 5);
WriteCard(i * i * i, 7);
WriteLn
END
END squares.
The control variable i is an ordinary variable declared in
the VAR section. The field widths of WriteCard
line the numbers up in columns.
$ gm2 squares.mod -o squares $ ./squares n n*n n*n*n 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216
Steps with BY
BY sets the step of a FOR loop. In Wirth's
definition the step is a constant expression; a negative step counts
down.
MODULE steps;
FROM InOut IMPORT WriteString, WriteLn, WriteCard, WriteInt;
VAR
minute: CARDINAL;
level: INTEGER;
BEGIN
WriteString("Every quarter:");
FOR minute := 0 TO 60 BY 15 DO
WriteCard(minute, 3)
END;
WriteLn;
WriteString("Countdown:");
FOR minute := 5 TO 0 BY -1 DO
WriteCard(minute, 2)
END;
WriteLn;
WriteString("Levels:");
FOR level := 6 TO -6 BY -4 DO
WriteInt(level, 3)
END;
WriteLn;
WriteString("Empty loop:");
FOR minute := 10 TO 1 DO
WriteCard(minute, 3)
END;
WriteLn
END steps.
A cardinal can count down to 0 with BY -1: the loop checks
the limit before it steps, so it never goes below zero. When the start is
already past the limit, as in the last loop, the body does not run.
$ gm2 steps.mod -o steps $ ./steps Every quarter: 0 15 30 45 60 Countdown: 5 4 3 2 1 0 Levels: 6 2 -2 -6 Empty loop:
Looping over characters
The control variable may have any whole-number, character or enumeration
type. A loop over CHAR visits the characters in the order of
their codes.
MODULE alphabet;
FROM InOut IMPORT WriteLn, Write;
VAR
ch: CHAR;
i: CARDINAL;
BEGIN
FOR ch := 'a' TO 'z' DO
Write(ch)
END;
WriteLn;
FOR ch := 'a' TO 'z' DO
Write(CAP(ch))
END;
WriteLn;
FOR i := 25 TO 0 BY -5 DO
Write(CHR(ORD('a') + i))
END;
WriteLn
END alphabet.
The second loop turns each letter to upper case with CAP.
GNU Modula-2 15.2 rejects a BY step in a loop over
characters, so the last loop counts with a cardinal and converts each
number to a letter with CHR.
$ gm2 alphabet.mod -o alphabet $ ./alphabet abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ zupkfa
INC and DEC
The standard procedures INC and DEC increase and
decrease a variable. With one argument they change it by one; a second
argument gives the amount.
MODULE counters;
FROM InOut IMPORT WriteString, WriteLn, WriteCard, WriteInt, Write;
VAR
visits: CARDINAL;
floor: INTEGER;
seat: CHAR;
BEGIN
visits := 0;
INC(visits);
INC(visits);
INC(visits, 10);
WriteString("Visits: "); WriteCard(visits, 0); WriteLn;
floor := 2;
DEC(floor, 3);
WriteString("Floor: "); WriteInt(floor, 0); WriteLn;
seat := 'C';
INC(seat);
WriteString("Next seat: "); Write(seat); WriteLn;
DEC(seat, 3);
WriteString("Three back: "); Write(seat); WriteLn
END counters.
INC(visits) is short for visits := visits + 1.
Both procedures also work on characters, where they move to the next or
previous code, which + cannot do on a CHAR.
$ gm2 counters.mod -o counters $ ./counters Visits: 12 Floor: -1 Next seat: D Three back: A
LOOP and EXIT
LOOP statements END repeats its body without a condition. An
EXIT statement, usually inside an IF, leaves the
innermost LOOP. It suits loops whose exit test falls in the
middle of the body.
MODULE total;
FROM InOut IMPORT WriteString, WriteLn, WriteInt, ReadInt, Done;
VAR
n, sum, count: INTEGER;
BEGIN
sum := 0;
count := 0;
LOOP
ReadInt(n);
IF NOT Done OR (n = 0) THEN
EXIT
END;
INC(sum, n);
INC(count)
END;
WriteString("Read ");
WriteInt(count, 0);
WriteString(" numbers, sum ");
WriteInt(sum, 0);
WriteLn
END total.
Each pass reads a number and then decides whether to stop, so the test
sits between the read and the addition. The loop ends at a 0 or at input
that is not a number. The numbers come from echo through a
pipe. In GNU Modula-2 15.2, ReadInt waits for more input when
it reaches the end of the input, so the input must contain a stop value.
$ gm2 total.mod -o total $ echo "4 8 15 16 23 42 0" | ./total Read 6 numbers, sum 108
Nested loops
A loop body may contain another loop. The inner loop runs completely for every pass of the outer one.
MODULE table;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
CONST
Size = 6;
VAR
row, col: CARDINAL;
BEGIN
WriteString(" |");
FOR col := 1 TO Size DO
WriteCard(col, 4)
END;
WriteLn;
WriteString("---+------------------------");
WriteLn;
FOR row := 1 TO Size DO
WriteCard(row, 2);
WriteString(" |");
FOR col := 1 TO Size DO
WriteCard(row * col, 4)
END;
WriteLn
END
END table.
The outer loop prints the rows and the inner loop the columns of a multiplication table. The two loops need different control variables. The heading line uses a loop of its own.
$ gm2 table.mod -o table $ ./table | 1 2 3 4 5 6 ---+------------------------ 1 | 1 2 3 4 5 6 2 | 2 4 6 8 10 12 3 | 3 6 9 12 15 18 4 | 4 8 12 16 20 24 5 | 5 10 15 20 25 30 6 | 6 12 18 24 30 36
The limit of a FOR loop
The start and the limit of a FOR loop are evaluated once,
before the first pass. Changing a variable used in them does not change the
number of passes.
MODULE limits;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
VAR
i, last: CARDINAL;
BEGIN
last := 3;
WriteString("Steps:");
FOR i := 1 TO last DO
WriteCard(i, 2);
last := 10
END;
WriteLn;
WriteString("last is now ");
WriteCard(last, 0);
WriteLn
END limits.
The loop runs three times, although last is set to 10 in the
first pass. The body must not assign to the control variable itself, and
its value after the loop is undefined in Wirth's definition; a program
that needs the final count keeps it in another variable.
$ gm2 limits.mod -o limits $ ./limits Steps: 1 2 3 last is now 10
Prime numbers
The last program combines the loops of this article. A FOR
loop goes through the candidates, and a WHILE loop inside it
looks for a divisor.
MODULE primes;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
CONST
Limit = 60;
VAR
n, d, found: CARDINAL;
prime: BOOLEAN;
BEGIN
found := 0;
FOR n := 2 TO Limit DO
prime := TRUE;
d := 2;
WHILE prime AND (d * d <= n) DO
IF n MOD d = 0 THEN
prime := FALSE
END;
INC(d)
END;
IF prime THEN
WriteCard(n, 3);
INC(found);
IF found MOD 10 = 0 THEN
WriteLn
END
END
END;
WriteLn;
WriteString("Primes up to ");
WriteCard(Limit, 0);
WriteString(": ");
WriteCard(found, 0);
WriteLn
END primes.
A number is prime when no d with d * d <= n
divides it. The WHILE condition stops the search as soon as a
divisor is found. The counter found starts a new line after
every ten primes.
$ gm2 primes.mod -o primes $ ./primes 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 Primes up to 60: 17
Next steps
Loops and conditionals are the building blocks of procedures, which give a name to a group of statements. The procedures article comes next in the series.
Source
Modula-2 language reference at modula2.org
In this article we have repeated statements in Modula-2 with
WHILE, REPEAT, FOR and
LOOP, changed counters with INC and
DEC, and nested loops. The
conditionals article covers the
IF statements used inside the loops, and the
operators article the conditions that
control them.
Author
List all Modula-2 tutorials.