Modula-2 operators
last modified November 26, 2026 Modula-2
An operator combines values into a new value: a + b adds two
numbers and a < b compares them. This article covers the
arithmetic, comparison and boolean operators of Modula-2, the way they
behave with negative numbers and zero, and the precedence rules that decide
how an expression is grouped. Set operators are covered in the sets
article.
Arithmetic operators
The arithmetic operators on whole numbers are +,
-, *, DIV for the integer quotient and
MOD for the remainder. A minus sign in front of a value negates
it.
MODULE arithmetic;
FROM InOut IMPORT WriteString, WriteLn, WriteInt;
VAR
a, b: INTEGER;
BEGIN
a := 17;
b := 5;
WriteString("a + b = "); WriteInt(a + b, 0); WriteLn;
WriteString("a - b = "); WriteInt(a - b, 0); WriteLn;
WriteString("a * b = "); WriteInt(a * b, 0); WriteLn;
WriteString("a DIV b = "); WriteInt(a DIV b, 0); WriteLn;
WriteString("a MOD b = "); WriteInt(a MOD b, 0); WriteLn;
WriteString("-a = "); WriteInt(-a, 0); WriteLn
END arithmetic.
DIV and MOD are keywords, so they are written in
upper case like the rest. Seventeen divided by five is three with a
remainder of two.
$ gm2 arithmetic.mod -o arithmetic $ ./arithmetic a + b = 22 a - b = 12 a * b = 85 a DIV b = 3 a MOD b = 2 -a = -17
Real division
The / operator divides real numbers. A whole-number division
uses DIV, which drops the fraction, and MOD
returns what is left.
MODULE division;
FROM InOut IMPORT WriteString, WriteLn, WriteInt;
FROM RealInOut IMPORT WriteReal;
VAR
distance, hours: INTEGER;
km, time: REAL;
BEGIN
distance := 250;
hours := 3;
WriteString("Whole km per hour: ");
WriteInt(distance DIV hours, 0);
WriteLn;
WriteString("Remaining km: ");
WriteInt(distance MOD hours, 0);
WriteLn;
km := 250.0;
time := 3.0;
WriteString("Exact speed: ");
WriteReal(km / time, 0);
WriteLn
END division.
The same journey is computed twice: once with integers, which gives 83 km
per hour with 1 km left over, and once with reals, which gives the exact
speed. GNU Modula-2 also accepts / on two integers and
truncates the result, but Wirth's definition reserves / for
reals and other compilers reject it.
$ gm2 division.mod -o division $ ./division Whole km per hour: 83 Remaining km: 1 Exact speed: 83.333333
Negative operands
With negative numbers the rules for DIV and MOD
differ between definitions of the language. GNU Modula-2 also has the ISO
operator REM, which it accepts in the default PIM mode as
well.
MODULE negative;
FROM InOut IMPORT WriteString, WriteLn, WriteInt;
VAR
a, b: INTEGER;
BEGIN
a := -7;
b := 2;
WriteString("-7 DIV 2 = "); WriteInt(a DIV b, 0); WriteLn;
WriteString("-7 MOD 2 = "); WriteInt(a MOD b, 0); WriteLn;
WriteString("-7 REM 2 = "); WriteInt(a REM b, 0); WriteLn;
a := 7;
b := -2;
WriteString("7 DIV -2 = "); WriteInt(a DIV b, 0); WriteLn;
WriteString("7 MOD -2 = "); WriteInt(a MOD b, 0); WriteLn;
WriteString("7 REM -2 = "); WriteInt(a REM b, 0); WriteLn;
WriteString("Literal -7 DIV 2 = "); WriteInt(-7 DIV 2, 0); WriteLn
END negative.
GNU Modula-2 15.2 chooses the quotient so that MOD is never
negative: -7 is -4 times 2 plus 1. REM truncates the quotient
towards zero, so its result takes the sign of the dividend. The last line
prints -3 because a leading minus sign has a lower precedence than
DIV: the literal expression means -(7 DIV 2).
Programs that must give the same results on every compiler avoid
DIV and MOD on negative operands. In this version
a build with -fsoft-check-all wrongly aborts on a negative
dividend with a "floor division will cause a range overflow" message.
$ gm2 negative.mod -o negative $ ./negative -7 DIV 2 = -4 -7 MOD 2 = 1 -7 REM 2 = -1 7 DIV -2 = -3 7 MOD -2 = 1 7 REM -2 = 1 Literal -7 DIV 2 = -3
Division by zero
Dividing a whole number by zero has no result. The program stops at the division, before the rest of the line is printed.
MODULE zero;
FROM InOut IMPORT WriteString, WriteLn, WriteInt;
VAR
total, count: INTEGER;
BEGIN
total := 120;
count := 0;
WriteString("Average: ");
WriteInt(total DIV count, 0);
WriteLn;
WriteString("Done");
WriteLn
END zero.
The runtime system reports an illegal whole value exception and aborts the
program; the shell receives exit status 134. The
echo $? command prints the exit status of the last program.
$ gm2 zero.mod -o zero $ ./zero Average: RTExceptions.mod:686:35: In wholediv RTExceptions.mod:686:35:illegal whole value exception Aborted (core dumped) $ echo $? 134
Mixing types
The operands of an arithmetic operator must have compatible types.
INTEGER and CARDINAL are different types, so they
cannot be added directly, and neither can a REAL and an
INTEGER.
MODULE mixing;
FROM InOut IMPORT WriteString, WriteLn, WriteInt;
VAR
balance: INTEGER;
deposit: CARDINAL;
BEGIN
balance := -40;
deposit := 100;
WriteString("New balance: ");
WriteInt(balance + deposit, 0);
WriteLn
END mixing.
The compiler rejects the sum. A literal such as 100 fits both
types, so balance + 100 would compile; the problem is only the
variable of the other type. The
type conversions article shows how
to convert one operand.
$ gm2 mixing.mod -o mixing
mixing.mod:13:20: error: In program module ‘mixing’: type incompatibility between ‘CARDINAL’ and ‘INTEGER’
13 | WriteInt(balance + deposit, 0);
| ~~~~~~~~^~~~~~~~~
mixing.mod:7:3: error: variable declaration for ‘deposit’
7 | deposit: CARDINAL;
| ^~~~~~~~~~~~~~~~~
mixing.mod:6:3: error: variable declaration for ‘balance’
6 | balance: INTEGER;
| ^~~~~~~~~~~~~~~~
Comparison operators
The comparison operators are =, # or
<> for not equal, <, <=,
> and >=. Each returns a
BOOLEAN, printed here as 0 or 1 with ORD.
MODULE comparison;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
VAR
x, y: INTEGER;
first, second: CHAR;
BEGIN
x := 4;
y := 9;
WriteString("x = y "); WriteCard(ORD(x = y), 0); WriteLn;
WriteString("x # y "); WriteCard(ORD(x # y), 0); WriteLn;
WriteString("x <> y "); WriteCard(ORD(x <> y), 0); WriteLn;
WriteString("x < y "); WriteCard(ORD(x < y), 0); WriteLn;
WriteString("x <= y "); WriteCard(ORD(x <= y), 0); WriteLn;
WriteString("x > y "); WriteCard(ORD(x > y), 0); WriteLn;
WriteString("x >= y "); WriteCard(ORD(x >= y), 0); WriteLn;
first := 'B';
second := 'a';
WriteString("'B' < 'a' "); WriteCard(ORD(first < second), 0); WriteLn
END comparison.
# and <> mean the same thing; Wirth's book
uses #. Characters are compared by their codes, so the
upper-case B (66) comes before the lower-case a
(97).
$ gm2 comparison.mod -o comparison $ ./comparison x = y 0 x # y 1 x <> y 1 x < y 1 x <= y 1 x > y 0 x >= y 0 'B' < 'a' 1
Boolean operators
AND, OR and NOT combine boolean
values. & is another spelling of AND, and
~ of NOT.
MODULE logic;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
VAR
age, income: INTEGER;
adult, student, earns, discount: BOOLEAN;
BEGIN
age := 22;
income := 400;
student := TRUE;
adult := age >= 18;
earns := income > 1000;
discount := student OR NOT earns;
WriteString("adult AND student: ");
WriteCard(ORD(adult AND student), 0); WriteLn;
WriteString("adult & earns: ");
WriteCard(ORD(adult & earns), 0); WriteLn;
WriteString("NOT earns: ");
WriteCard(ORD(NOT earns), 0); WriteLn;
WriteString("~student: ");
WriteCard(ORD(~student), 0); WriteLn;
WriteString("discount: ");
WriteCard(ORD(discount), 0); WriteLn
END logic.
The program decides whether a person gets a discount: students always do,
and so does anyone who does not earn more than 1000. NOT
applies only to the value right after it.
$ gm2 logic.mod -o logic $ ./logic adult AND student: 1 adult & earns: 0 NOT earns: 1 ~student: 0 discount: 1
Short-circuit evaluation
AND and OR evaluate their right operand only when
it is needed. If the left operand of AND is
FALSE, the result is FALSE whatever the right
side is, and if the left operand of OR is TRUE,
the result is TRUE.
MODULE shortcircuit;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
VAR
total, count: INTEGER;
high: BOOLEAN;
BEGIN
total := 0;
count := 0;
high := (count # 0) AND (total DIV count > 50);
WriteString("Guarded AND: ");
WriteCard(ORD(high), 0);
WriteLn;
high := (count = 0) OR (total DIV count > 50);
WriteString("Guarded OR: ");
WriteCard(ORD(high), 0);
WriteLn;
high := (total DIV count > 50) AND (count # 0);
WriteString("Unguarded: ");
WriteCard(ORD(high), 0);
WriteLn
END shortcircuit.
The first two expressions test count before dividing by it,
so the division never runs. The third expression divides first, and the
program aborts. Putting the guard on the left is a common Modula-2 idiom;
Pascal does not promise this order of evaluation.
$ gm2 shortcircuit.mod -o shortcircuit $ ./shortcircuit Guarded AND: 0 Guarded OR: 1 RTExceptions.mod:686:35: In wholediv RTExceptions.mod:686:35:illegal whole value exception Aborted (core dumped)
Precedence
Modula-2 has only four precedence levels, from the highest:
NOTand~;- the multiplying operators
*,/,DIV,MOD,REM,ANDand&; - the adding operators
+,-andOR, and a leading sign; - the relations
=,#,<>,<,<=,>,>=andIN.
Operators on the same level are applied from left to right.
MODULE precedence;
FROM InOut IMPORT WriteString, WriteLn, WriteInt, WriteCard;
VAR
a, b: INTEGER;
p, q: BOOLEAN;
BEGIN
a := 5;
b := 3;
WriteString("2 + 3 * 4 = "); WriteInt(2 + 3 * 4, 0); WriteLn;
WriteString("(2 + 3) * 4 = "); WriteInt((2 + 3) * 4, 0); WriteLn;
WriteString("20 DIV 2 * 5 = "); WriteInt(20 DIV 2 * 5, 0); WriteLn;
WriteString("-a MOD b = "); WriteInt(-a MOD b, 0); WriteLn;
p := FALSE;
q := TRUE;
WriteString("NOT p AND q = "); WriteCard(ORD(NOT p AND q), 0); WriteLn;
WriteString("p OR q AND p = "); WriteCard(ORD(p OR q AND p), 0); WriteLn;
WriteString("(a > 0) AND (b > 0) = ");
WriteCard(ORD((a > 0) AND (b > 0)), 0);
WriteLn
END precedence.
20 DIV 2 * 5 is (20 DIV 2) * 5, because the two
operators share a level. -a MOD b is -(a MOD b),
because the sign belongs to the adding level. AND binds
tighter than OR, just as * binds tighter than
+, so p OR q AND p is p OR (q AND p).
The comparisons in the last expression need their parentheses, as the next
section shows.
$ gm2 precedence.mod -o precedence $ ./precedence 2 + 3 * 4 = 14 (2 + 3) * 4 = 20 20 DIV 2 * 5 = 50 -a MOD b = -2 NOT p AND q = 1 p OR q AND p = 0 (a > 0) AND (b > 0) = 1
A precedence trap
Because AND is a multiplying operator, it binds tighter than
a comparison. An expression that would be fine in C or Java fails in
Modula-2.
MODULE trap;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
VAR
a, b: INTEGER;
both: BOOLEAN;
BEGIN
a := 5;
b := 3;
both := a > 0 AND b > 0;
WriteString("Both positive: ");
WriteCard(ORD(both), 0);
WriteLn
END trap.
The compiler reads a > 0 AND b > 0 as
a > (0 AND b) > 0, and two relations in a row are a
syntax error. Writing (a > 0) AND (b > 0) fixes it.
Every comparison combined with AND or OR needs
its own parentheses.
$ gm2 trap.mod -o trap
trap.mod:12:23: error: In program module ‘trap’: syntax error, found ‘>’
12 | both := a > 0 AND b > 0;
| ^
trap.mod:17:1: error: no scope active: compilation failed
ABS and ODD
Some common operations are standard functions rather than operators.
ABS returns the absolute value of an integer or a real, and
ODD tests whether a whole number is odd.
MODULE functions;
FROM InOut IMPORT WriteString, WriteLn, WriteInt, WriteCard;
FROM RealInOut IMPORT WriteReal;
VAR
change, year: INTEGER;
error: REAL;
BEGIN
change := -12;
year := 2027;
error := -0.25;
WriteString("ABS(change): "); WriteInt(ABS(change), 0); WriteLn;
WriteString("ABS(error): "); WriteReal(ABS(error), 0); WriteLn;
WriteString("ODD(year): "); WriteCard(ORD(ODD(year)), 0); WriteLn;
WriteString("ODD(year + 1): "); WriteCard(ORD(ODD(year + 1)), 0); WriteLn;
WriteString("MAX(INTEGER) - 1: "); WriteInt(MAX(INTEGER) - 1, 0); WriteLn
END functions.
Standard functions need no import, like the operators. ABS
returns a value of the same type as its argument, so it works on both
change and error.
$ gm2 functions.mod -o functions $ ./functions ABS(change): 12 ABS(error): 0.250000 ODD(year): 1 ODD(year + 1): 0 MAX(INTEGER) - 1: 2147483646
Next steps
The type conversions article shows how to combine integers, cardinals and reals in one expression. The conditionals article uses comparisons and boolean operators to choose between statements.
Source
Modula-2 language reference at modula2.org
In this article we have covered the operators of Modula-2: arithmetic with
DIV, MOD and REM, real division,
comparisons, boolean operators with short-circuit evaluation, and the four
precedence levels. The basics article
introduced the arithmetic operators, and the
data types article describes the types
they work on.
Author
List all Modula-2 tutorials.