For-Loop
The For-Loop statement allows you to repeat a set of actions any specified number of times.
The For-Loop statement is located under Common Statements in the Assets Panel.
To specify a for-loop, you need to specify:
-
The loop parameters that control how many cycles will be performed
-
The conditions that must be met for the current loop to be performed
-
The actions to perform during each cycle (the body of the loop)
Parameters
The following parameters must be specified:
Parameter |
Input Type |
Description |
---|---|---|
Iterator Name | Text | The name of the loop iterator, by default Iterator |
Initial Value | Number | The initial value of the iterator |
Increment | Number | The value to increment the iterator by at the end of each loop |
Conditions
Specify the conditions that must be met for the loop to execute the current loop. If the conditions are not met, the loop ends.
These conditions are specified in the same way as the conditions for an If statement are specified. See If.
Note that the conditions can be based on the current value of the iterator. The iterator is listed in the Selector window.
Body
Specify the actions to be performed during each cycle.
Note that the current value of the iterator can be used to populate parameters of the actions in the body of the for-loop. The iterator is listed in the Selector window.
Example
The workflow step shown below includes a for-loop followed by a single action.
The for-loop:
-
Names its iterator i.
-
Sets the initial value of i to 1.
-
Raises the value of i by 2 at the end of each cycle.
-
Only begins a cycle if i is smaller than 20.
-
In each cycle, adds the current value of i to the list List_Values.
When the for-loop ends, the last action is performed: the value 999 is added to the list List_Values. This action is performed just once because it is not within the for-loop.
After the workflow is run, the values in List_Values are as below:
Note that:
-
In the first cycle, the value of i was the initial value specified, 1.
-
The iterator was incremented only at the end of the first cycle.
-
The last cycle was performed when i had a value of 19. After that cycle, i was incremented by 2 to 21, but because the for-loop conditions were not met, no more cycles were performed.
-
After the for-loop ended, the next action was performed and the value of 999 was added to the list.
Nesting For-Loops
Nested for-loops are supported. In other words, you can add a for-loop into the body of another for-loop.
The image below shows three levels of for-loop nesting.
Within the body of any of the for-loops, the iterator of that loop and of the loops above it are available in the Selector window.
For example, if you click the left parameter of the Assign function in the middle loop, only Iterator_A and Iterator_B are listed. The iterator of the lowest for-loop, Iterator_C, is not listed.
For-Loops vs. Workflow Loops
In many cases, it is possible to achieve the same outcome using a workflow loop instead of a for-loop. For example, the workflow below achieves the same outcome as the for-loop shown in the example above.
The speed of a for-loop far exceeds that of a workflow loop, so for-loops should be used instead of workflow loops whenever possible.