Expressions - Operators
The following binary operators apply to all signed and unsigned integer types:
Both operands of a binary operator must be compatible; that is, you cannot add an INTEGER to a CARDINAL. You can use type conversion procedures or type transfers to combine operands of different types.
Unary + can be used on all the types. Unary - can be used on signed integer types.
The result type of all the integer operators is the same as the type of the operands. If the result of the operator is outside the allowable range of the type, overflow occurs.
Stony Brook M2 users: The result of an overflow depends on the qualifiers used when compiling. If overflow checking is on, a runtime error is signaled, otherwise, the low-order bits of the result are produced without any error.
The following binary operators apply to operands of type REAL and LONGREAL:
Both operands of an operator must be the same type.
Unary + and unary - also apply to both real types.
The result type of all the real operators is the same as the operands.
The following operators apply to Boolean types. This includes the extended syntax boolean types, however the result of those expressions still return type BOOLEAN.
The operators are defined as follows:
All the boolean operators produce results of type BOOLEAN.
The following operators apply to set types. Both operands must be the same set type:
The operations are defined as follows:
Set union is equivalent to a bit-by-bit OR operation.
Set intersection is equivalent to a bit-by-bit AND operation.
Set symmetric difference is equivalent to a bit-by-bit XOR operation.
These operators are the arithmetic comparison operators and the set inclusion and membership operators.
The six arithmetic relations apply to all signed and unsigned integer types, REAL, LONGREAL, BOOLEAN, CHAR, and enumeration types.
Order of Evaluation
A precedence is associated with each operator that determines the order in which operators are applied in an expression. Operators of higher precedence are applied before operators of lower precedence. Operators of the same precedence are evaluated in left-to-right order.
You can use parentheses in an expression to override the order of evaluation. A parenthesized expression is always completely evaluated before being used as an operand.
There are four levels of precedence for Modula-2 operators. They are, from highest to lowest:
Level 4: NOT ~
Level 3: * / REM DIV MOD AND &
Level 2: + - OR
Level 1: < <= = >= > <> # IN
2 + 3 * 5
equals 17, not 25, because the multiplication operator (*) has higher precedence than the addition.
(2 + 3) * 5
equals 25 because the parentheses force the addition to be performed before the multiplication.