Branching serves to accommodate different reactions depending on a particular condition.
You can perform branching using the appropriate if
construct or a switch
. Note that the constructs represent an expression block.
The if-then-end returns the value returned by the <expression>
if the Boolean_expression is true and null
if the Boolean_expression is false.
if <boolean_expression> then <expression> end
Example
//sendInfo is a Boolean variable. if sendInfo then "Do send the newsletter." end //if sendInfo is false, the expression returns null (consider exception handling).
The if-then-else-end returns the value returned by <expression_1>
if the Boolean_expression is true and value returned by <expression_2>
if the Boolean_expression is false.
if <boolean_expression> then <expression_1> else <expression_2> end
Example:
//passedTest is a Boolean variable. if passedTest then "Passed" else "Failed" end
boolean_expression_1
evaluates to false, boolean_expression_2
is checked.boolean_expression_2
is true, expression_2
is evaluated, and the evaluation leaves the if construct.boolean_expression_2
is false, the next elsif expression is checked, etc. If none of the elsif Boolean expression is true, expression_N
is evaluated, and the evaluation leaves the if construct.if <boolean_expression_1> then <expression_1> elsif <boolean_expression_2> then <expression_2> end
Example
//passedTest is a String variable. if passedTest=="yes" then "Passed" elsif passedTest=="no" then "Failed" end
if <boolean_expression_1> then <expression_1> elsif <boolean_expression_2> then <expression_2> else <else_expression> end
Example:
//passedTest is a String variable. if passedTest=="yes" then "Passed" elsif passedTest=="no" then "Failed" else "Did not attend" end
The switch
construct branches the execution flow based on condition value: it compares the argument expression against multiple possible values. If the value of the argument expression matches the value of the case, the expression defined for that case is executed and the switch
returns the value of the expression. Unlike in Java, every case expression has an implicit break
.
You can define a default
expression that is executed if none of the cases matches is executed.
switch month case "January" -> 1 case "February" -> 2 default -> "Not January nor February" end
Looping serves to repeat the same or similar action.
Note that the looping constructs represent an expression block block.
The for loop, server to loop through a block of expressions, until a condition becomes true.
for(init; condition; update) do expression end
Example
def Integer i := 0; for ( i; i < 10; i++) do debugLog({-> toString(i)}, 1000) end
Important: Collecting results of
foreach
,for
, andwhile
in a collection so that you create a new collection on each iteration as below, is inefficient and can cause performance issues (consider that collections are immutable):def Integer i := 0; def Set<Integer> varSet := {}; for ( i; i < 10; i++) do //creates a new set with the i added and assigns it to varSet: varSet := add(varSet, i); end //varSet will contain { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }Use
collect()
,fold()
,exist()
,forAll()
, etc. of the Standard Library instead. For example:collect(1..10, { x:Integer -> new Option(label -> "Option " + x, value -> "Value " + x) }) //instead of: //def List<Option> options := []; //foreach Integer x in 1..10 do // options := add(options, new Option(label -> "Option " + x, value -> "Value " + x) ) //end; //options;
To iterate through items in a collection, use foreach
:
foreach <type> <iterator_name> in <collection> do <expression> end
Example:
def Set<Person> persons := { ... }; foreach Person person in persons do sendEmail("Important Notification", "", {} , {person.email}, {}, {}, "UTF-8"); end
To loop code while an expression is true, use the while
construct:
while <boolean_expression> do <expression> end
In while, for, and foreach loops, you can use the break keyword to finish the looping immediately and continue with the next expression.
def Integer i := 0; for ( i; i < 10; i++) do if i = 3 then break; end end
In while, for, and foreach loops, you can skip the current loop with the continue keyword.
def Set<Person> persons := {}; foreach Person person in persons do if isEmpty(person.email) then continue; end; sendEmail("Important Notification", "", {} , {person.email}, {}, {}, "UTF-8"); end