Cyclic Statements

Iteration statements repeatedly execute nested statements in cyclic statements. The following iterations are available in the Fore.NET language:

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.

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.

Within the For statement one can use the Break statement to exit the cycle, and the Continue statement to pass control to the end point of nested cycle statements and execute the further cycle work iteration.

Example

Sub TestFor();
Var
    i, j: integer;
    Rnd: Random = New Random();
Begin
    For i := 0 To 100 Do
        j := Rnd.Next(0100);
        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.

The For Each statement enumerates the elements of a collection, executing the nested statement block for each collection element.

Each For Each statement includes the identifier of the variable named iteration variable. During the statement execution the iteration variable represents a collection element, for which iteration is currently being executed. A compilation error occurs if the nested statement attempts to modify the iteration variable or pass the iteration variable as a reference parameter.

The expression in the statement (its type) must be a collection, and an explicit conversion must be defined from the element type of the collection to the type of the iteration variable.

The System.Array system type is also a collection type. Since all array types derive from this type, any array type expression is permitted in the For Each statement. The order, in which array elements are traversed, is as follows. For one-dimensional arrays, elements are traversed in increasing index order from the least to the greatest. For multidimensional arrays, elements are traversed such that the indexes of the rightmost dimension are increased first, then the next left dimension, and the leftmost dimension indexes are increased last.

Example

Sub TestForEach();
Var
    s: Array 
Of String = New String[5];
    s1: String;
    i: Array[
0..50..5Of Integer;
    i1: Integer;
Begin
    
//...
    //Fill with values of arrays s and i
    //...
    //Handle array elements in the For Each cycle
    For Each s1 In s Do
        
//Operations with s1
    End For;
    For Each i1 In i Do
        
//Operations with i1
    End For;
End Sub;

The Repeat statement executes nested statements one or more times depending on a condition.

The statement is executed as follows:

Within the Repeat statement one can use the Break statement to exit the cycle, and the Continue statement to pass control to the end point of the nested cycle statements and execute the further cycle work iteration.

Example

Sub TestRepeat();
Var
    b: boolean;
Begin
    Repeat
        //Code executed in cycle
        If < Condition for exiting cycle > Then
            b := True;
        End If;
    Until b;
End Sub;

The While statement conditionally executes nested statements, depending on the condition, zero or more times.

The statement is executed as follows:

Within the While statement one can use the Break statement to exit the cycle, and also the Continue statement to pass control to the end point of nested cycle statements and execute the further cycle work iteration.

Example

Sub TestWhile();
Var
    b: boolean;
Begin
    b := True;
    While b Do
        If < Condition of exiting cycle > Then
            b := False;
        End If;
    End While;
End Sub;

See also:

Fore.NET Language Guide