Statements denote algorithmic actions, and are said to be executable.
Stat ::= AssignStat | CondStat | LoopStat | ProcStat
StatSeq ::= Stat { ';' Stat }
AssignStat ::= Var ':=' ExprExamples:
i := i+1 r := r*3.141592 b := i<1 a[4][2] := rThe variable and the expression must be of identical type, with the following exception being permitted: The type of the variable is REAL, and the type of the expression is INTEGER. In any case, the variable must be of a simple type.
In the case of a value parameter, the actual parameter must be an expression (of which a variable is a simple case). The corresponding formal parameter represents a local variable of the called procedure, and the current value of the expression is initially assigned to this variable. Value parameters must have a simple type. In the case of a variable parameter, the actual parameter must be a variable of the same type, and the corresponding formal parameter represents this actual variable during the entire execution of the procedure. If this variable is a component of an array, its index is evaluated when the procedure is called. A variable parameter must be used whenever the parameter represents a result of the procedure.
ProcStat ::= Id [ '(' Expr { ',' Expr } ')' ]Examples:
next Transpose(a,m,n)
CondStat ::= 'IF' Expr 'THEN' StatSeq 'ELSE' StatSeq 'END'Examples:
IF i < 0 THEN i := 1 ELSE i := 2 ENDThe expression between the delimiters IF and THEN must be of type Boolean.
LoopStat ::= 'WHILE' Expr 'DO' StatSeq 'END'The expression controlling repetition must be of type Boolean. The statement is repeatedly executed as long as the expression is true. If it evaluates to false at the beginning, the statement is not executed at all. The while statement
WHILE b DO s ENDis equivalent to
IF b THEN s; WHILE b DO s END ELSE (* nothing *) ENDExamples:
WHILE a [i] < r DO i := i + 1 END WHILE i < n DO r := 2 * r; i := i + 1 END
ProcDecl ::= ProcHead ';' Block Block ::= 'DECLARE' Decl { ';' Decl } 'BEGIN' StatSeq 'END' Decl ::= VarDecl | ProcDeclThe procedure heading specifies the identifier naming the procedure and the formal parameter identifiers (if any). The parameters are either value or variable parameters.
ProcHead ::= 'PROCEDURE' Id [ '(' Formal { ';' Formal } ')' ] Formal ::= [ 'VAR' ] Id ':' TypeIf a formal starts with the delimiter VAR it specifies a variable parameter, otherwise a value parameter.
The statement sequence of the block specifies the algorithmic actions to be executed upon an activation of the procedure by a procedure statement.
All identifiers introduced in the formal parameter part of the procedure heading and in the declaration part of the associated block are local to the procedure declaration which is called the scope of these identifiers. They are not known outside their scope. In the case of local variables, their values are undefined at the beginning of the statement part.
The use of the procedure identifier in a procedure statement within its declaration implies recursive execution of the procedure.
Examples of procedure declarations:
PROCEDURE ReadPosInteger (VAR i: INTEGER); DECLARE j: INTEGER; BEGIN i := 0; WHILE NOT (0 < i) DO READ (i) END END PROCEDURE Sort (VAR a: ARRAY [1..10] OF REAL; n: INTEGER); DECLARE i: INTEGER; j: INTEGER; k: INTEGER; h: REAL; BEGIN i := 1; WHILE i < n DO (* a [1], ... , a [i] is sorted *) j := i; k := i; WHILE j < n DO (* a [k] = min {a [i], ... , a [j]} *) j := j + 1; IF a [j] < a [k] THEN k := j ELSE k := k END END; h := a [i]; a [i] := a [k]; a [k] := h; i := i + 1 END END