PhixFlow Help
Conditional Statements
Controlling flow
This page describes the elements used to control the flow of a script i.e. how 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 provide one way to generate a true or false value which can then be used by the script to make a decision. Other logical operators are documented here. 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(). |
Please note that within PhixFlow, true is represented by (and is indistinguishable from) the integer 1. Similarly false is represented by 0.
Note that a common mistake is 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 (&& and ||)
Often several different conditions need to be combined with the logical functions && (logical and) and || (logical or). 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". |
Note that the normal logical processing precedence applies with && (and()) conditions evaluated before || (or()). Use brackets () around conditions to alter the processing logic.
You should also be aware of the fact that PhixFlow processes logical expressions from left to right and 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 | Result |
---|---|
if( in.balance < 0, "negative", // else   "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. |
Please let us know if we could improve this page feedback@phixflow.com