for each
for each statements iterate over a collection, executing a block of
statements for each element.
Syntax
for each <type> in <expression> do
<statements>
end
type- The variable name for the current element.
expression- The collection to iterate over.
statements- The code block executed for each element.
end- All
for eachstatements must terminate with theendkeyword.
Example
The following example iterates through a list of numbers:
for each number in {1, 2, 3, 4, 5} do
set result to result + this number + " ";
end
// Output: "1 2 3 4 5 "
It works as follows:
- Iterates through each element in the collection
{1, 2, 3, 4, 5}. - Assigns the current element to
number. - Appends the value to
resultwith a space. - Repeats for all elements in order.