ZetCode

Modula-2 conditionals

last modified November 26, 2026 Modula-2

A conditional statement runs a group of statements only when a condition holds. Modula-2 has two of them: IF, which tests boolean expressions, and CASE, which selects a branch by the value of an expression. Both end with END, so a branch may hold any number of statements without BEGIN and END around them, as Pascal requires.

The IF statement

The simplest form is IF condition THEN statements END. The statements run when the condition is TRUE; otherwise the program continues after END.

frost.mod
MODULE frost;

FROM InOut IMPORT WriteString, WriteLn, WriteInt;

VAR
  temperature: INTEGER;

BEGIN
  temperature := -4;
  WriteString("Temperature: ");
  WriteInt(temperature, 0);
  WriteLn;
  IF temperature < 0 THEN
    WriteString("Frost warning");
    WriteLn
  END
END frost.

The branch holds two statements, separated by a semicolon. The keyword END closes the IF, so there is no question which statements belong to it.

$ gm2 frost.mod -o frost
$ ./frost
Temperature: -4
Frost warning

IF and ELSE

An ELSE part runs when the condition is FALSE. Exactly one of the two branches runs.

parity.mod
MODULE parity;

FROM InOut IMPORT WriteString, WriteLn, WriteCard;

VAR
  seats: CARDINAL;

BEGIN
  seats := 37;
  WriteCard(seats, 0);
  IF seats MOD 2 = 0 THEN
    WriteString(" seats can be split into pairs")
  ELSE
    WriteString(" seats leave one person alone")
  END;
  WriteLn
END parity.

The condition seats MOD 2 = 0 needs no parentheses: the MOD is computed first and its result is compared. The final WriteLn stands after END, so it runs in both cases.

$ gm2 parity.mod -o parity
$ ./parity
37 seats leave one person alone

ELSIF chains

ELSIF adds another condition to the same statement. The conditions are tested from the top, and only the branch of the first true condition runs.

grades.mod
MODULE grades;

FROM InOut IMPORT WriteString, WriteLn, WriteCard, Write;

VAR
  score: CARDINAL;
  grade: CHAR;

BEGIN
  score := 78;
  IF score >= 90 THEN
    grade := 'A'
  ELSIF score >= 80 THEN
    grade := 'B'
  ELSIF score >= 70 THEN
    grade := 'C'
  ELSIF score >= 60 THEN
    grade := 'D'
  ELSE
    grade := 'F'
  END;
  WriteString("Score ");
  WriteCard(score, 0);
  WriteString(" gets grade ");
  Write(grade);
  WriteLn
END grades.

A score of 78 fails the first two tests and passes score >= 70. The later conditions are not tested at all, so each one may assume that the earlier ones were false: the score >= 70 branch knows that the score is below 80. A single END closes the whole chain.

$ gm2 grades.mod -o grades
$ ./grades
Score 78 gets grade C

Combined conditions

Conditions are combined with AND, OR and NOT. Every comparison needs its own parentheses, because AND binds tighter than =; the operators article explains why.

leapyear.mod
MODULE leapyear;

FROM InOut IMPORT WriteString, WriteLn, WriteCard;

VAR
  year: CARDINAL;
  leap: BOOLEAN;

BEGIN
  year := 1900;
  leap := (year MOD 4 = 0) AND (year MOD 100 # 0) OR (year MOD 400 = 0);
  WriteCard(year, 0);
  IF leap THEN
    WriteString(" is a leap year")
  ELSE
    WriteString(" is not a leap year")
  END;
  WriteLn;

  year := 2028;
  IF (year MOD 4 = 0) & ((year MOD 100 # 0) OR (year MOD 400 = 0)) THEN
    WriteCard(year, 0);
    WriteString(" is a leap year");
    WriteLn
  END
END leapyear.

A year is a leap year when it is divisible by 4 but not by 100, or when it is divisible by 400. The first test stores the result in a boolean variable, which is then used as the condition. The second test writes the same rule directly into the IF, with & for AND and extra parentheses around the OR.

$ gm2 leapyear.mod -o leapyear
$ ./leapyear
1900 is not a leap year
2028 is a leap year

Nested IF statements

A branch may contain another IF. Each inner statement has its own END, so an ELSE always belongs to the IF it is written under; Modula-2 has no dangling ELSE problem.

ticket.mod
MODULE ticket;

FROM InOut IMPORT WriteString, WriteLn, WriteCard;

VAR
  age, price: CARDINAL;
  weekend: BOOLEAN;

BEGIN
  age := 15;
  weekend := TRUE;
  IF age < 18 THEN
    IF weekend THEN
      price := 6
    ELSE
      price := 4
    END
  ELSE
    IF weekend THEN
      price := 12
    ELSE
      price := 9
    END
  END;
  WriteString("Ticket price: ");
  WriteCard(price, 0);
  WriteString(" EUR");
  WriteLn
END ticket.

The outer IF tests the age and the inner ones the day. A 15-year-old visitor on a weekend pays 6 EUR. Indentation shows the structure to the reader, but the compiler relies only on the END keywords.

$ gm2 ticket.mod -o ticket
$ ./ticket
Ticket price: 6 EUR

Testing input

InOut.ReadInt reads a whole number from the keyboard. It sets the variable Done to TRUE when it found a number and to FALSE otherwise, so the program can check the input with an IF.

checknum.mod
MODULE checknum;

FROM InOut IMPORT WriteString, WriteLn, WriteInt, ReadInt, Done;

VAR
  n: INTEGER;
  ok: BOOLEAN;

BEGIN
  WriteString("Enter a whole number: ");
  ReadInt(n);
  ok := Done;
  IF NOT ok THEN
    WriteString("That is not a whole number")
  ELSIF n < 0 THEN
    WriteString("Negative, distance from zero is ");
    WriteInt(-n, 0)
  ELSIF n = 0 THEN
    WriteString("Zero")
  ELSE
    WriteString("Positive, double is ");
    WriteInt(2 * n, 0)
  END;
  WriteLn
END checknum.

Done is copied to ok right after the read, because the output procedures of InOut, such as WriteString, set Done as well. The first branch handles bad input, so the later branches can use n safely. In GNU Modula-2 15.2, ReadInt waits for more input when it reaches the end of the input instead of returning; type the number and press Enter.

$ gm2 checknum.mod -o checknum
$ ./checknum
Enter a whole number: -12
Negative, distance from zero is 12
$ ./checknum
Enter a whole number: abc
That is not a whole number

The CASE statement

CASE selects a branch by the value of an expression of a whole-number, character, boolean or enumeration type. Each branch starts with one or more labels, and the branches are separated by |.

weekday.mod
MODULE weekday;

FROM InOut IMPORT WriteString, WriteLn, WriteCard;

VAR
  day: CARDINAL;

BEGIN
  day := 6;
  WriteString("Day ");
  WriteCard(day, 0);
  WriteString(" is ");
  CASE day OF
    1: WriteString("Monday")
  | 2: WriteString("Tuesday")
  | 3: WriteString("Wednesday")
  | 4: WriteString("Thursday")
  | 5: WriteString("Friday")
  | 6, 7: WriteString("on the weekend")
  END;
  WriteLn
END weekday.

The labels must be constants. 6, 7 is a list of labels that share one branch. A CASE is clearer than a long ELSIF chain that compares one variable with constants, and the compiler can turn it into a jump table.

$ gm2 weekday.mod -o weekday
$ ./weekday
Day 6 is on the weekend

Ranges and ELSE

A label may be a range low..high. An ELSE part at the end handles every value that no label lists.

classify.mod
MODULE classify;

FROM InOut IMPORT WriteString, WriteLn, Write;

VAR
  ch: CHAR;

BEGIN
  ch := 'k';
  Write(ch);
  WriteString(" is ");
  CASE ch OF
    'a', 'e', 'i', 'o', 'u':
      WriteString("a lower-case vowel")
  | 'b'..'d', 'f'..'h', 'j'..'n', 'p'..'t', 'v'..'z':
      WriteString("a lower-case consonant")
  | 'A'..'Z':
      WriteString("an upper-case letter")
  | '0'..'9':
      WriteString("a digit")
  ELSE
    WriteString("something else")
  END;
  WriteLn
END classify.

The consonants are listed as ranges that skip the vowels, because no value may appear under two labels. The ELSE branch takes spaces, punctuation and every other character. Unlike the other branches, it starts without a label and the | separator.

$ gm2 classify.mod -o classify
$ ./classify
k is a lower-case consonant

When no label matches

A CASE without ELSE may get a value that no label covers. Wirth's definition treats this as an error, but what happens depends on how the program was compiled.

nomatch.mod
MODULE nomatch;

FROM InOut IMPORT WriteString, WriteLn, WriteCard;

VAR
  month: CARDINAL;

BEGIN
  month := 13;
  WriteString("Month ");
  WriteCard(month, 0);
  WriteString(": ");
  CASE month OF
    12, 1, 2: WriteString("winter")
  | 3..5: WriteString("spring")
  | 6..8: WriteString("summer")
  | 9..11: WriteString("autumn")
  END;
  WriteLn
END nomatch.

Month 13 matches no label. The default build skips the CASE and continues, so the program prints nothing after the colon. With -fsoft-check-all the program aborts at the end of the statement. Add an ELSE whenever the selector can take a value that the labels do not list.

$ gm2 nomatch.mod -o nomatch
$ ./nomatch
Month 13: 
$ gm2 -fsoft-check-all nomatch.mod -o nomatch
$ ./nomatch
Month 13: nomatch.mod:18:6: In program module nomatch
nomatch.mod:18:6:this CASE statement does not have an ELSE statement
Aborted (core dumped)

Overlapping labels

Every value may appear in at most one label. The compiler checks this, so two ranges that share a value are rejected.

duplicate.mod
MODULE duplicate;

FROM InOut IMPORT WriteString, WriteLn;

VAR
  floor: INTEGER;

BEGIN
  floor := 3;
  CASE floor OF
    0: WriteString("lobby")
  | 1..5: WriteString("offices")
  | 5..9: WriteString("apartments")
  ELSE
    WriteString("no such floor")
  END;
  WriteLn
END duplicate.

Floor 5 belongs to both 1..5 and 5..9. The fix is to decide where 5 belongs and change one of the ranges, for example to 6..9.

$ gm2 duplicate.mod -o duplicate
duplicate.mod:12:8: error: case range ‘1’..‘5’ overlaps case range ‘5’..‘9’
   12 |   | 1..5: WriteString("offices")
      |         
duplicate.mod:13:8: error: case range ‘5’..‘9’ overlaps case range ‘1’..‘5’
   13 |   | 5..9: WriteString("apartments")
      |         

Next steps

The loops article repeats statements with WHILE, REPEAT, FOR and LOOP, and uses conditionals to leave a loop early.

Source

The GNU Modula-2 manual

Modula-2 language reference at modula2.org

In this article we have chosen between statements in Modula-2 with IF, ELSIF and ELSE, tested input read with InOut.ReadInt, and selected branches with CASE, label lists, ranges and ELSE. The operators article covers the comparisons and boolean operators used in conditions, and the data types article introduces the BOOLEAN type.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Modula-2 tutorials.