The For statement executes embedded operators the specified number of times. The iteration number 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 transferred to the embedded statement block. When the end point of the embedded statement block is reached, the value of the control variable is changed by the iteration step value and the control is transferred 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 transferred to the end point of the statement.
Within the embedded statement block of the For statement one can use the Break statement to immediately transfer control to the end point of the For statement, and the Continue statement to transfer control to the end point of embedded statements.
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 loop exit is executed as if a random integer value greater than 50 was generated.
See also: