Executes a block of statements for all elements in a collection.
FOREACH [IMPLIED <idElement> | VAR <idElement> | <idElement> AS <idType>] IN <container>
<Statements>...
[EXIT]
<Statements>...
[LOOP]
NEXT
In the FoxPro dialect, FOR EACH is also allowed as two separate words.
In the FoxPro and Xbase++ dialect, ENDFOR is allowed as an alternative for NEXT.
<idElement> | The name of the variable that will receive the values of the elements in <container> When the IMPLIED or VAR clause is used, the datatype of the variable is inferred from the container. When the AS <idType> clause is used, this will be the datatype of the variable. |
AS <idType> | Specifies the data type. If omitted, then depending on the compiler options the type will be either USUAL or determined by the compiler. |
<container> | A DotNet object that supports IEnumerable(), such as a XBase array, .Net array or a collection like List<>. |
EXIT | Unconditionally branches control from within a FOR, FOREACH , REPEAT or DO WHILE statement to the statement immediately following the corresponding ENDDO or NEXT statement. |
LOOP | Branches control to the most recently executed FOR, FOREACH , REPEAT or DO WHILE statement. |
The FOREACH statement is a convenient way to enumerate variable in an array or collection.
Preferably, the iteration variable should not be changed inside the loop. A compiler warning will be shown when you do this.
It is also not recommended to change the container in the loop as this may often result in a runtime error.
AEval(), BEGIN SEQUENCE, DO CASE, DO WHILE, IF, EXIT, LOOP