modula-2 home

  Home  
  Tutorial  
  Win32 API  
  Reference  
  Projects  
 

 

Expressions - Operators

Integer operators

The following binary operators apply to all signed and unsigned integer types:

+Addition
-Subtraction
*Multiplication
/Integer division
REMInteger remainder
DIVDivision with modulus
MODInteger modulus operation

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.

Real operators

The following binary operators apply to operands of type REAL and LONGREAL:

+Addition
-Subtraction
*Multiplication
/Division

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.

Boolean operators

The following operators apply to Boolean types. This includes the extended syntax boolean types, however the result of those expressions still return type BOOLEAN.

(NOT, ~)Boolean negation
(AND, &)Boolean conjunction
ORBoolean disjunction

The operators are defined as follows:

NOT Ais TRUE if A is FALSE, FALSE if A is TRUE.
A AND Bis TRUE if and only if both A and B are TRUE.
A OR Bis TRUE if and only if either A is TRUE or B is TRUE.

All the boolean operators produce results of type BOOLEAN.

Set operators

The following operators apply to set types.  Both operands must be the same set type:

+set union
-set difference
*set intersection
/set symmetric difference

The operations are defined as follows:

A + Bthe set of elements in either A or B
A - Bis the set of elements that are in A and are not in B
A * Bis the set of elements in both A and B
A / B is the set of elements that are in either A or B but not in both A and B

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.

Relational operators

These operators are the arithmetic comparison operators and the set inclusion and membership operators.

=equal
(<>, #)not equal
<less
>greater
<=less or equal, set inclusion
>=greater or equal, set inclusion
INset membership

The six arithmetic relations apply to all signed and unsigned integer types, REAL, LONGREAL, BOOLEAN, CHAR, and enumeration types.

  • The equal and not equal operators also apply to SET, POINTER, COMPLEX, LONGCOMPLEX and procedure types.
  • A <= B, where A and B are compatible sets indicates that B contains all of the elements of A, that is, A is a subset of B.
  • A >= B, where A and B are compatible sets indicates that A contains all the elements of B, that is, B is a subset of A.
  • A IN B, where B is a set type and A is a value in the base type of the set, indicates that element A is in the set B.
  • All the relational operators produce results of type BOOLEAN.

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

Examples:

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.


Source:

  • Stony Brook Modula-2 documentation. Used with permission. Note: Stony-Brook M2 offers an extended syntax with features not described here. Stony Brook M2 users are encouraged to visit the Stony Brook website and to consult the Stony Brook help system.