if



Function: if()

Process an Expression (and return its value) depending on whether a condition evaluates to True or False.

Syntax

  1. if(condition, trueExpression)
  2. if(condition, trueExpression, falseExpression)
ArgumentTypeDescription
conditionBooleanThe condition to be checked
trueExpressionAnyThe expression to be evaluated if condition is True
falseExpressionAnyAn optional expression to be evaluated if condition is False

Note that if you want to process more than a one-line expression if condition evaluates to True or False then wrap the script in a do() function. See the examples below.

Examples

if( in.UserID == "Test", 0, in.numCalls)

Returns the value of in.numCalls unless in.UserID is equal to "Test".


if( startsWith(in.telNo,"44"), 
	do ( 
		$priceBand = "NATIONAL", 
		$rate = 0.03 
	)
)

This example shows how to use a do() function if you want to process several expressions (a script) based on the result of an if(). Note that the $-Variables $priceBand and $rate are only set if in.telNo starts with "44".

A common mistake is to omit the do() function in cases like the previous example, as shown below:

if( startsWith(in.telNo,"44"), $priceBand = "NATIONAL", $rate = 0.03 )

With this expression, only $priceBand = "NATIONAL" is evaluated if the condition returns True, and only $rate = 0.03 is evaluated if the condition returns False.


if( startsWith(in.telNo,"44"), 
   
   do( $priceBand = "NATIONAL", $rate = 0.03 )
      
      ,// Else 
   
   do( $priceBand = "INTERNATIONAL", $rate = 0.60 ) 

)

This example extends the example above to include an "else" clause i.e. the expressions that should be processed if the condition returns False.

Note the layout in the last example. Consistent formatting of your scripts like this can help clarify where the "else" script starts. Also see Expression Style.

See Also