Foreach statement
Description
The foreach
statement repeats a group of embedded statements for each element
in an array. The foreach statement is used to iterate
through the collection to get the desired information, but should
not be used to change the contents of the collection to avoid
unpredictable side effects.
Syntax
for(type
identifier in Array)
statement
endforeach
The foreach statement syntax has
these parts:
|
Part
|
Description
|
|
type
|
The type of
array variable.
|
|
identifier
|
The iteration
variable that represents the array element.
|
|
Array
|
An array
expression
|
|
statement
|
The statement
to be executed. Can be a compound statement.
|
Remarks
The foreach operation works
significantly faster than for loop so you should use it as
much as possible when working with arrays. Be sure however that
target array contains variables of the same type. The following
example demonstrates a foreach loop.
array Array
for(int
i=0;i<100;i++)
Array[i] =
i
endfor
foreach (int a
in Array)
Println(a)
endforeach
|