Overview
This page describes the conditional statements that you can use to control the flow of a script. Use these conditions to change which statements are evaluated based on a set of conditions. The main two script functions used to control flow are if() and switch().
Comparison Operators
The comparison operators are:
- == exactly equal to
- != not equal to
- > greater than
- >= greater than or equal to
- < less than
- <= less than or equal to.
Use comparison operators to generate a true
or false
value. Your expression or script uses the value to make a decision. For more logical operators, see Logical Functions.
Examples of these functions in use are:
Expression | Result |
---|---|
in.productId == 3 | Evaluates to true if the id of the product is exactly equal to 3. Otherwise the expression returns false. == is just another notation for the function eq(). |
in.productId != 3 | Evaluates to true if the id of the product is not equal to 3. != is just another notation for the function ne(). |
in.productId > 3 | Evaluates to true if the id of the product is greater than 3. > is just another notation for the function gt(). |
in.productId >= 3 | Evaluates to true if the id of the product is greater than or equal to 3. >= is just another notation for the function ge(). |
in.productId < 3 | Evaluates to true if the id of the product is less than 3. > is just another notation for the function lt(). |
in.productId <= 3 | Evaluates to true if the id of the product is less than or equal to 3. <= is just another notation for the function le(). |
Within PhixFlow, true
is represented by, and is indistinguishable from, the integer 1. Similarly false
is represented by 0.
Remember to use the assignment operator "=" instead "==" when comparing two values. If $a = $b is written instead of $a == $b then the expression evaluates to the value in $b. If this is null or 0, then the value is treated as false. Otherwise the value is treated as true.
not() and !
The result of a logical expression can be reversed by using the not() function or more simply prefix the expression with "!".
Expression | Result |
---|---|
not(in.productId == 3) | Evaluates to false if the id of the product is exactly equal to 3. Otherwise the expression returns true. |
!(in.productId == 3) | This is exactly the same as the expression above. |
Combining Logical Values
Often several different conditions need to be combined with the logical functions:
For example:
Expression | Result |
---|---|
in.productId == 3 && in.custType == "BUS" | Evaluates to true if the id of the product is exactly equal to 3 and the customer type is "BUS". |
in.productId == 3 || in.custType == "BUS" | Evaluates to true if the id of the product is exactly equal to 3 or the customer type is "BUS". |
The normal logical processing precedence applies with && (and()) conditions evaluated before || (or()). Use brackets () around conditions to alter the processing logic.
PhixFlow processes logical expressions:
- from left to right
- stops processing as soon as it has evaluated enough of the expression to determine if the result will be true or false.
This can be important when an expression includes a reference to a lookup pipe. If the expression is determined to be true before the lookup pipe is reached, then the lookup pipe will not fire. This processing order is important when performance tuning models.
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 | if( in.balance < 0, "negative", "positive" ) |
---|---|
Result | 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 | if( in.balance < 0, $modifier= "(debit)" ) |
---|---|
Result | 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 | if ( in.balance < 0, do ( $error = "Balance for account " + in.accountRef, $error = $error + " is less than zero", $credit = 0 ) ) |
---|---|
Result | 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 | switch ( [in.balance > 100, $creditRating = "A" ], [in.balance > 50, do ( $creditRating = "B", $promoCode = "boost" ) ], [in.balance > 0, $creditRating = "C" ], // Default $creditRating = "D" ) |
---|---|
Result | 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. |