Loop over a collection
The for-each-loop
is configured by a collection (a List or a Map) and a so called locator
variable. The collection is expressed by a List or Map expression which shall be evaluated initially. The flow of the loop
shall be executed repeatedly for each item
found in the collection
. The current item of an iteration can be accessed by using the locator
variable which contains the index or key of the List or Map item, respectively. Like this, the current item can be used for any kind of computation in the loop
flow.
- Note
- If the collection is a List, there is a defined order for the iterations: The loop starts with the first List item (index 0), followed the second item and so on. By contrast, there is no particular order defined for Map.
-
Since a
locator
variable is visible procedure-wide, it is accessible on the outside of the loop as well.
-
When entering a
for-each-loop
, the old value of the locator
variable shall be overwritten implicitly by the index/key of the first item of the List or Map. Similarly, the old value of the value variable shall be overwritten implicitly by the first item/value. After leaving the loop, the locator
variable value of the final iteration will be visible to the follower nodes of the loop node.
-
Actions derived from ListModifier and MapModifier are collection modifiers. Since they can change a List length or Map size, applying such modifications on a
for-each-loop
's collection during loop execution may cause serious inconsistencies (e.g. when a collection item is accessed which does not exist anymore). Therefore OTX runtime will throw ConcurrentModificationException when such modifications occur. By contrast, changing the value of items of a for-each-loop
's collection does not represent a concurrent modification and will not cause a ConcurrentModificationException.
- Syntax
{
...
}
{
...
}
{
...
}
Variable locator
This element must be an Integer or String Variable that will be used as the locator variable of the f...
Definition: Core.cs:5356
Variable value
This element points to a variable that shall hold the list items or map values.
Definition: Core.cs:5366
CollectionTerm collection
List or Map over which the for-each-loop iterates.
Definition: Core.cs:5376
- Examples
List<Integer> Collection1 = {1, 2, 3};
Integer Locator1;
Integer Value1;
Integer ValueAtLocator;
Integer ValueOfValue;
foreach (Locator1, Value1 in Collection1) : ForEachLoop1
{
ValueAtLocator = Collection1[Locator1];
ValueOfValue = Value1;
}
foreach (Locator1, Collection1) : ForEachLoop2
{
}
foreach (Value1 in Collection1) : ForEachLoop3
{
}
- Checker rules
- CheckerRule.Core_Chk014 - Correct locator variable type in for-each-loop
CheckerRule.Core_Chk056 - No modification of collection inside foreach-loops
CheckerRule.Core_Chk063 - Locator or value in for-each-loop
CheckerRule.Core_Chk064 - Correct value variable type in for-each-loop
- Exceptions
-
Exceptions.ConcurrentModificationException | It is thrown in case that the collection is modified while the loop is active. Modifications can be triggered by actions inside of the loop or from the outside, e.g.by actions in a parallel lane. |