Contents | Prev | Next | Index
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 a variable. This variable is called the control variable of the for statement, and it cannot be a component of a structured type, nor can it be imported or be a parameter. It must be of a non-real basic type, of an enumeration, or of a subrange type.
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 or of a cardinal 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] ENDFOR i:=79 TO 1 BY -1 DO a[i] := a[i-1] END
Contents | Prev | Next | Index
Canterbury Modula-2 for Java (Last documentation update
Feb 8, 2000)
Copyright © 1998 Mill Hill &
Canterbury Corporation, Ltd. All rights reserved
Please send any comments or corrections to
mhc@webcom.com