Script Loops

Overview

In this tutorial you will learn:

  • how to use the loops to repeat parts of a script

The foreach Loop

The foreach loop looks like the following:

foreach var in iterable
do
    ...
endloop

The loop starts with the foreach keyword. Next follows the loop variable var that may changed to a different name. After the in keyword an iterable must be specified, usually an array. The loop sets var to each value in the iterable and executes the part of the script bewteen the do and endloop keywords until the last item in the iterable is reached.

You may want to try the following example in the Script-IDE of FlowModeler:

values = [ 1, 2, 3, 4, 5 ]

foreach val in values
do
    print( val )
endloop

The while Loop

The while loop looks like the following:

while condition
do
    ...
endloop

The loop starts with a while keyword and the part between the do and endloop keywords is executed repeatedly until condition evaluates to false.

Please be aware that this type of loop may run forever if condition never evaluates to false (infinite loop).

You may want to try the following example in the Script-IDE of FlowModeler:

i = 5

while i greater 0
do
    print( i )
    i = i - 1
endloop

Conclusion

Loops execute a part of script repeatedly. There are two types of loops: the foreach loop iterates over a predefined set of variables, the while loop repeats as long a specified condition evaluates to true.

Downloads