Show/Hide Toolbars

XSharp

Purpose

Execute a block of statements for all elements in a collection

Syntax

FOREACH  [IMPLIED <idElement> | VAR <idElement> | <idElement> AS <idType>] IN <container>
  <Statements>...
  [EXIT]
  <Statements>...
  [LOOP]
NEXT

Note

In the FoxPro dialect FOR EACH as 2 separate words is also allowed.

In the FoxPro and Xbase++ dialect ENDFOR is allowed as alternative for NEXT

Arguments

<idElement>The name of the variable that will receive the values of the elements in <container> When the IMPLIED or VAR clause is used then the datatype of the variable is inferred from the container. When the AS <idType> clause is used then 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<>

 

EXITUnconditionally branches control from within a FOR, FOREACH , REPEAT or DO WHILE statement to the statement immediately following the corresponding ENDDO or NEXT statement.

 

LOOPBranches control to the most recently executed FOR, FOREACH , REPEAT or DO WHILE statement.

Description

The FOREACH statement is a convenient way to enumerate variable in an array or collection.

Preferrably 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.

See Also

AEval(), BEGIN SEQUENCE, DO CASE, DO WHILE, IF, EXIT, LOOP