Returns the result of one of two expressions depending upon the result of a conditional expression.

Returns: Varies
Usage: Script or steady state.
Function Groups: Logic Control
Related to: IfElse Operator | Cond | Execute | IfThen

IfElse(Condition, TRUECase, FALSECase)


Condition ? TRUECase : FALSECase

Required. An expression that returns true or false. If true the TRUECase is executed. If false, the FALSECase is executed. If invalid, neither case is executed.
Required. The expression (typically an Execute statement) which is executed when Condition is true.
Required. The expression (typically an Execute statement) which is executed when the Condition is false.
Comments: This is simply a different name for the Cond function. The return value is the result of the evaluated case. Both cases are evaluated, in which case Cond is a more apt name.

Nesting many IfElse statements (or the IFElse operator) is poor programming practice. Use a Cond statement or look for alternatives such as the following.

A common example is a need to display one of a list of messages depending on a tag's value. (Note: the following example is written for a unilingual expression in a tag or page. In a module, do not refer to a tag using the bracket notation. )


An IfElse in an expression might look like:

[<TagName>] == 1 ? "Message 1" :
[<TagName>] == 2 ? "Message 2" :
[<TagName>] == 10 ? "Message 10" : "Error"

This can be replaced by a set of application properties, "Message1", Message2", etc. and a single-line expression.

PickValid(Variable(Concat("Message", [<TagName>])), "ERROR")

The Variable function will look for the first object named MessageN, where N is the current value of [<TagName>]. If this matches one of your application properties, the value stored in that property will be returned. Otherwise "Error" is returned. To add more messages, you need only add more properties.

Scripting example:

If 1 Main;
  IfElse(i > 0 && i <= 50, 
         Execute(y[i] = x, 
  { else } 
         Execute(x = i,
                 doneFlag = 1) 

This statement will set the element of array y equal to i and then increment i if i is between 1 and 50 inclusive. If i is not in that range, x will be set to i and doneFlag will go true. Note that Execute may only appear in a script.