Flow control

The Workflow language provides two flow control statements.

  1. foreach loops that makes it easy to iterate over lists and collections including statements such as break and continue to transfer the flow of execution to another point in your code.

  2. if statements to execute different branches of code based on certain conditions; 

The foreach loop

You use the foreach loop to iterate over a sequence, such as items in a list, or in a collection.

This example uses a foreach loop to iterate over the items in an array:

//Create a list var gods as list of string; //Add some items to the list gods[] = "Saturn"; gods[] = "Venus"; gods[] = "Minerva"; var joined as string; //Loop over the list foreach god in gods { joined = joined + god + ", "; } //joined now contains "Saturn, Venus, Minerva, "

You can also use foreach loops with numeric ranges, by using the loop in conjunction with the system->sequence() function. This example prints the first few entries in a five-times table:

var result as string; //Loop over the list foreach i in system->sequence(1, 5, 1) { result = result + "(" + i + " x 5) = " + (i * 5) + ", "; } //result now contains "(1 x 5) = 5, (2 x 5) = 10, (3 x 5) = 15, (4 x 5) = 20, (5 x 5) = 25"

The if conditional

In its simplest form, the if statement has a single if condition. It executes a set of statements only if that condition is true.

var input = "find the needle in the haystack"; var result as string; if (string->contains(input, "needle")) { result = "Found the needle!"; } //result contains "Found the needle!"

The example above checks whether the input phrase contains the string "needle". If it's found, a message written in the result variable. Otherwise, nothing happens, and code execution continues after the if statement’s closing brace.

The if statement can provide an alternative set of statements, known as an else clause, for situations when the if condition is false. These statements are indicated by the else keyword.

The open and close braces are optional if in the true or false branches you have a single statement. Here is the same example as before, but without braces:

This is is useful for brevity, and for creating multiple condition branches within a if statement, as in the folowing example:

As we can observe the second if statement is actually the single statement of the first if statement. Now, so that it looks like a single if statement with multiple condition branches, we will add back the braces and rearrange it a bit: