if()
The if() function allows the user to specify if a statement should be evaluated based on the result of an expression. A typical use of if() is to create a one line expression to return one value or another based on a condition. For example:
Expression | Result |
---|---|
if( in.balance < 0, "negative", "positive" ) | This returns the string "negative" if in.balance is less than zero otherwise the expression returns "positive" |
Another way to write this expression is as follows:
if( in.balance < 0, $modifier = "negative" , // else $modifier = "positive" )
We recommend this second form when you are writing more complex scripts however if all you want to do is set the value of a Stream Attribute to one value or another based on a simple logical condition, then the first, one-line form is fine.
It is not necessary to have both the if and else parts of this if() statement. For example:
Expression | Result |
---|---|
if( in.balance < 0, $modifier= "(debit)" ) | This just sets the $-variable $modifier to "(debit)" if in.balance is less than zero |
Multiple statements
If you need to evaluate multiple statements based on the outcome of a condition, then group the statements together in a do function. For example:
Expression | Result |
---|---|
if ( in.balance < 0, do ( $error = "Balance for account " + in.accountRef, $error = $error + " is less than zero", $credit = 0 ) ) | All the statements within the do() function are treated as one and are evaluated if in.balance<0. $error is set to something like "Balance for account 123456 is less than zero" and $credit is set to zero. |
switch()
The switch() function is used when you want to have several different flows depending on various conditions. For example:
Expression | Result |
---|---|
switch ( [in.balance > 100, $creditRating = "A" ], [in.balance > 50, do ( $creditRating = "B", $promoCode = "boost" ) ], [in.balance > 0, $creditRating = "C" ], // Default $creditRating = "D" ) | in.balance is first checked to see if it is greater than 100. If it is then the associated statement is evaluated and $creditRating is set to "A" and no other part of the expression is evaluated. If in.balance is greater than 50, again the credit rating is set but this time a promotion code is also set - this is to illustrate that if you want to have multiple statements within a switch() condition you can group them in a do() function. Finally the switch statement has the (mandatory) Default statement. This statement must always be present (even if it only contains a _NULL statement). It is a good idea to always include the // Default comment to highlight the entry. |