The For Statement

Syntax

$ ForStatement = FOR ident ":= " expression1 TO expression2 [STEP expression3] DO StatementSequence END FOR ";"

Description

The For statement is used for an enclosed sequence of operator cycling. The statement algorithm is as follows. On initial appearance in the cycle values of expression1, expression2 and expression3 expressions are calculated. All expressions values must be integers. Initially, the ident counter value is expression1. Then, till the counter exceeds the expression2 value, the statements sequence is executed and a counter increases its value by the expression3 value (or by 1, if the Step value is not defined). If the value of expression3 is negative, then the ident counter will be reduced with each iteration of the cycle until it becomes less than the expression2 value. When the counter reaches the expression2 value, statements in the cycle are executed for the last time and control is passed to the statement, which follows END FOR.

NOTE. If the expression3 expression is set to 0 on calculation, an exception is thrown.

Example

Sub ForSample;

Var

a, b: Integer;

Begin

a := 1;

b := 1;

For a := 1 To 3 Do

b := b * a

End For;

End Sub ForSample;

 

Sub ForStepSample;

Var

a, b: Integer;

Begin

a := 1;

b := 1;

For a := 1 To 10 Step 2 Do

b := b + a

End For;

End Sub ForStepSample;

See also:

Statements