The For statement executes nested operators the specified number of times. The number of iterations depends on the start and end value of the iteration variable and on its step.
for-statement:
For identifier := expression To expression for-step-clauseopt Do block End For
for-step-clause:
Step expression
Each For statement includes the identifier of the variable named the iteration variable. It also contains an expression that determines the start and end value of the iteration variable and the step of its change. If no iteration variable step is specified, it is considered to be equal to one. The statement is executed as follows:
The expression determining the start value of the iteration variable is calculated. A calculated value is assigned to the iteration variable. This step is executed only once when the statement execution is started.
The value of the expression determining the end value of the iteration variable is calculated and compared to the current iteration variable value. If the current value of the iteration variable is less or equal (or greater or equal) than the calculated value provided that the iteration step is positive (negative), the control is passed to the nested statement block. When the end point of the nested statement block is reached, the value of the control variable is changed by the iteration step value and the control is passed to this step from the beginning.
If the current value of the iteration variable is or greater (less) than the end value provided that the iteration step is positive (negative), the control is passed to the end point of the statement.
Within the cycle one can use the Break statement to simultaneously pass control to the end point of the For statement, and also the Continue statement to pass control to the end point of nested cycle statements and go to the nest cycle work iteration.
Private Sub TestFor();
Var
i, j: integer;
Rnd: Random = New Random();
Begin
For i := 0 To 100 Do
j := Rnd.Next(0, 100);
If j > 50 Then
Break;
End If;
End For;
End Sub;
On executing the example cycle exit is executed as if a random integer value greater than 50 was generated.
See also: