Script Branches

Overview

In this tutorial you will learn:

  • what a branch statement is
  • how to use use branches in your scripts

What is a branch

A script can have a part that is only executed when a specified condition is fulfilled. In flowScript branches are defined by if statements. The if statement may be accompanied by an else statement.

A single branch looks like this:

if condition
then
    ...
endif

The if keyword starts the branch. condition is an expression or a variable that must evaluate to true, then the part of the script between the then and endif keywords is evaluated or ignored otherwise.

A branch may be extended with an else branch:

if condition
then
    ...
else
    ...
endif

The part of the script between the else end the endif is evaluated in case the condition evaluates to false.

It is also possible to extend branches with multiple conditions:

if condition_1
then
    ...
else if condition_2
then
    ...
else
    ...
endif

The script above would check if any of the conditions evaluates to true and execute that part of the branch, otherwise the else branch is executed.

Branch by Example

Start FlowModeler, open the Script-IDE and try out the followng script:

number = 10

if number less 0
then
    print( number + " is negative")
else if number greater 0
then
    print( number + " is positive" )
else
    print( number + " is zero" )
endif

Modify the number and check the resulting message that gets printed for a negatve number, a positive number and when number is zero.

Conclusion

In this short tutorial you learned how to use the branch statement in flowScript. An if part of a branch is only executed when a condition evaluates to true. Branches can have multiple nested else if parts. Branches can have an optional else branch which is executed when none of the evaluated conditions was true. The else part of a branch is optional.

Downloads