Modula-2 Advanced Syntax Reference

Program Structure

MODULE ModuleName;

    (* This program does  ... *)  documentation, comment

    FROM IO IMPORT WrStr, WrLn;   import general procedures

    CONST PI = 3.1416;       constante-declarations

    TYPE HoursSb=[0...24];    type-declarations

    PROCEDURE Bla ...        procedures
        ...
    END Bla;

    VAR aNumber: HoursSb;    variabele-declarations

BEGIN
        ...                  statements
END ModuleName.
 

Single Types

Assignment: variable := value;
Cardinals, integers, booleans, chars, enums and subranges are ordinal types. Ordinal operators (like <, MAX, ...) apply.
All variables at module level are initialised with a 0 value, local variables of a procedure NOT!!

Structured Types

Procedures

PROCEDURE MyFunction(inputVariable1, VAR inputVariable2: CARDINAL; inputVariable3: WeekdayEn): BOOLEAN;
    VAR result: BOOLEAN;   variabele-declarations
BEGIN
    ...                   statements
    RETURN result;
END MyFunction;

Operators

Use round brackets (haakjes) to indicate evaluation priorities
  eg: IF x MOD 3 = 0 THEN   gives an error (the = operation is evaluated before the MOD)
  this should be: IF (x MOD 3) = 0 THEN

Control Statements

IF (a > 5) OR (b > 6) THEN      condition
    ...                   statements
ELSIF ( a > 3) THEN        optional
    ...                   statements
ELSE                   optional
    ...                   statements
END;

CASE day OF
    Monday: statements;   use a case label only once
    | Tuesday .. Thursday: statements;
    | Friday, Sunday: statements;
ELSE                     when none of the case labels apply
  statements
END;

WHILE NOT (i > 10) DO      condition
    statements...
END;

REPEAT
    statements...
UNTIL (i = 10);

LOOP
    statements...
    IF condition THEN
        EXIT
    END;
    statements...
END;

FOR  i := 10 TO 0 BY -1 DO     By clause is optional
    statements...
END;

FOR is possible with all ordinal types (like enums)!

Library IO (input - output)

Library RealMath (mathematical functions on reals)