Cycles are used to execute a specific code block while some condition is executed. There are the following types of cycles in Fore:
The For statement provides cyclic repetition of nested sequence of statements until the end counter value is reached. For cycle syntax:
For <Expression1> To <Expression2> Step <Expression3> Do
End For;
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. Specify some variable (cycle counter) and its initial value in the expression1 expression. Then, a sequence of statements is executed within the cycle until the counter exceeds expression2 values. The cycle counter increases by expression3 value (or by 1 if the Step keyword is missed). If the expression3 value is negative, the counter decreases with each cycle iteration until it is 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;
The For Each statement is used for cyclic repetition of a nested sequence of statements for each collection element or array element.
For Each cycle syntax:
For Each <variable> In <array> Do
End For;
The For Each cycle is executed if there is at least one element in the collection. After the cycle starts, statements are executed for the first collection element, and the element's value is inserted into the <variable> variable; if there are other elements in the collection, the statements in the cycle will be executed in series for all the elements. If there are no elements, the cycle stops and the statement, following the End For keywords, takes the control.
This statement is optimized for working with a dictionary elements array provided by the IDimElementArray. As a result, the collection dictionary element numbers will be returned in the cycle.
Example:
Sub ForEachSample;
Var
d: Double;
Arr: Array Of Double;
TabSheetBox1: TabSheetBox;
Begin
Arr := TabSheetBox1.Source.GetTabSheet.ParseRange("A0:K0").ToDoubleArray(True);
For Each d In Arr Do
Debug.Write(d.ToString + " ");
End For;
End Sub ForEachSample;
After executing the example the console window displays values of all non-empty cells in the range A0:K0.
Sub ForEachDimArray;
Var
MB: IMetabase;
DimInst: IDimInstance;
Elem: IDimElementArray;
i: Integer;
Begin
MB := MetabaseClass.Active;
DimInst := MB.ItemById("D_TO").Open(Null) As IDimInstance;
Elem := DimInst.RootElements;
For Each i In Elem Do
Debug.Write(i.ToString + " ");
End For;
End Sub ForEachDimArray;
After executing the example the console window displays indexes of all root elements of the dictionary with the D_TO identifier.
The Repeat statement provides cyclic repetition of nested sequence of statements until the value specified after the Until keyword becomes true. The expression value is checked after the sequence is executed. The expression value must be compatible with the Boolean type.
Example:
Sub RepeatSample;
Var
a, b, c: Integer;
Begin
a := 1;
b := 4;
c := 2;
Repeat
b := b + b * 5 - 25 * c - 3;
a := a + 1;
Until a = 10
End Sub RepeatSample;
The While statement provides cyclic repetition of nested sequence of statements while the value specified between the While and Do keywords is true. The expression value is checked before the sequence execution starts. The expression value must be compatible with the Boolean type.
Example:
Sub WhileSample;
Var
a, b, c: Integer;
Begin
a := 1;
b := 4;
c := 2;
While a < 10 Do
b := b + b * 5 - 25 * c - 3;
a := a + 1;
End While;
End Sub WhileSample;
See also: