Contents | Prev | Next | Index


For Statements

The for statement indicates that a statement sequence is to be repeatedly executed for a fixed number of times while a progression of values is assigned to an integer variable. This integer variable is called the control variable of the for statement.

for_stmt         ::= FOR control_list DO stmt_seq END
control_list     ::= control_assignment to_expr by_expr
control_assignment ::= control_var := expr
control_var      ::= <identifier>
to_expr          ::= TO expr
by_expr          ::= BY const_expr
                 ::= <empty>

The statement

FOR v := low TO high BY step DO statements END

is equivalent to

v := low; temp := high;
IF step > 0 THEN
  WHILE v <= temp DO statements; v := v+step END
ELSE
  WHILE v >= temp DO statements; v := v+step END
END

low must be assignment compatible with v, high must be expression compatible (that is, comparable) with v, and step must be a nonzero constant expression of an integer type. If step is not specified, it is assumed to have the default value 1. Examples:

FOR i:=0 TO 79 DO k := k+a[i] END

FOR i:=79 TO 1 BY -1 DO a[i] := a[i-1] END


Contents | Prev | Next | Index

Canterbury Oberon-2 for JVM  (Last documentation update Jun 2, 2000)
Copyright © 1998 Mill Hill & Canterbury Corporation, Ltd. All rights reserved
Please send any comments or corrections to mhc@webcom.com