For Statements

A For statement is a loop statement that keeps repeating for a predetermined number of times. There are various classes of For statement, both for the numeric For loops and enumerated For loops.

Numeric For Loop

A numeric For loop takes an existing Number datum, which must be a named local variable, and allocates a series of values to it.

forstmt ::= For datum From fromexpr To toexpr [ Step stepexpr ] Do stmtblock EndFor ;

A numeric For loop takes an existing Number datum, which must be a named local variable ( preferably Const ), and allocates a series of values to it. If using nested For loops you should use a different datum for each level. It is first given the result of the fromexpr. Each time around the loop an increment is added. If the Step is specified then that value is used - if not 1 is added or subtracted depending on whether the toexpr is higher or lower than the toexpr. If an explicit step is specified that goes in the opposite sense to the range limits then the loop will not execute but no error will be generated.

When the value has passed the toexpr ( in the direction of the sign of the step ) the loop will terminate. The value of the loop datum can be accessed within the loop ( and if not Const can be changed - but this is not recommended ). The values for all the expressions are evaluated once when the main loop statement is first executed. The control variable is set at the beginning of each loop to the correct next value ( ignoring and changes made within the loop ).

If you are using non-integer expressions you need to be aware of the possibility of rounding errors affecting the matching of the upper limit.

Program ()
	Const Number n;
    Begin
	For n From 10 To 100 Step 10 Do
		Circle(n) => {100,100};	
	EndFor;
    End;

Full Enumerated For Loop

A full enumerated For loop will assign a datum with all the values of an enumerated type in turn.

whilestmt ::= For datum In enumtype Do stmtblock EndFor ;

The datum must be a pre-declared datum of enumtype type.

Type MyEnum = ( Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec );
Program ()
	Const MyEnum e;
    Begin
	For e In MyEnum Do
		Output e;	
	EndFor;
    End;

Partial Enumerated For Loop

This is like the numeric For loop but with an enumerated type. The order and step go by the order in which the elements or the enumerated type were defined - any user specified numeric values are ignored. The step size must be an integer. If there is any doubt you should use the Round() function.

For e From May To Aug Do
	Output e;	
EndFor;