Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Insert excerpt
_Banners
_Banners
namephixScript
nopaneltrue

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:

ExpressionResult
in.productId == 3Evaluates 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 != 3Evaluates to true if the id of the product is not equal to 3. != is just another notation for the function ne().
in.productId > 3Evaluates to true if the id of the product is greater than 3. > is just another notation for the function gt().
in.productId >= 3Evaluates 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 < 3Evaluates to true if the id of the product is less than 3. > is just another notation for the function lt().
in.productId <= 3Evaluates 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.

Tip

Remember to use the equal to operator "==" instead of "=" when comparing two values. If $a = $b is written, instead of comparing if $a is equal to $b, $a is assigned the value of $b and so the value of $b is returned. 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 "!".

ExpressionResult
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:

  •  && - a logical and
  •  || - logical or.

For example:

ExpressionResult
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 &amp;&amp; (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:

Expressionif( in.balance < 0, "negative", "positive" )
ResultThis returns the string "negative" if in.balance is less than zero otherwise the expression returns "positive"

Another An alternative way to write this expression is as follows::

Code Block
languagejava
linenumberstrue
if( in.balance < 0, 
	$modifier = "negative" ,
// else 
	$modifier = "positive" 
)


Tip

We recommend

this second form when you are writing more complex scripts however if all you want to do is

using the above multi-line layout when writing expressions that involve any scripting.

If, however, you simply want to return a value, or set the value of an

attribute to one value or another

attribute based on a simple logical condition, then

the first, one-line form is fine

this can be placed on one line. See Expression Style.

It is not necessary to have both the if and else parts of this if() statement. For example:

Expression

Code Block
languagejava
linenumberstrue
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

Code Block
languagejava
linenumberstrue
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.

If you need to include an "else" clause in this expression, write it on a new line with an "// else" comment aligned with the "if " to make it easier to see the two different clauses. See Expression Style. For example:

Code Block
languagejava
linenumberstrue
if ( in.balance < 0,
  do (
    $error = "Balance for account " + in.accountRef,
    $error = $error + " is less than zero",
    $credit = 0
  ),
// else
  $error = _NULL
)

switch()

The switch() function is used when you want to have several different flows depending on various conditions. For example:

Code Block
languagejs
titleExpression
linenumberstrue
switch (
  [in.balance > 100,
    $creditRating = "A"
  ],

  [in.balance > 50,
    do (
      $creditRating = "B",
$promoCode

      $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 and this is always the last 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.


Code Block
languagejs
titleExpression
linenumberstrue
$creditRating =
switch (
  [in.balance > 100, "A"],
  [in.balance > 50, "B"],
  [in.balance > 0, "C"],
  // Default
  "D"
)

Result

The switch statement returns a value.

Tip

It is best practice to write a script on multiple lines to make it clearer.