IfElse

(Engine-Level Function)

Description:

Executes 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: Ternary Operator (IfElse) | Cond | Execute | IfThen
Format:

IfElse(Condition, TRUECase, FALSECase)

or

Condition ? TRUECase : FALSECase

Parameters:  
Condition   
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.
TRUECase   
Required. The expression (sometimes an Execute statement) that is executed when Condition is TRUE.
FALSECase   
Required. The expression (sometimes an Execute statement) that is executed when the Condition is FALSE.
Comments:

If you are interested in returning the value of the expression matching the TRUE case rather than conditionally executing the expression, refer to the Ternary Operator (IfElse) and Cond

Scripting example:

  If Watch(1, Value);
  [
    IfElse(Value > 50, Execute(
      ValueIsHigh = TRUE;
      HighValueCount++;
    );
    { Else }
      ValueIsHigh = FALSE;
    );
  ]

Writing long "If Else - Else - Else ... " expressions is not efficient and can be avoided. To prevent excessively nested expressions, the property MaxNestedExpressionDepth limits you to 200 function calls (including IfElse) within a single expression. There is no need to test this limit, regardless of the complexity of your application.

Alternatives to nested IfElse structures:

A common task is to build a string based on the current value of a tag (or several tags). For this example, suppose that you need to build a message or address that varies with a tag's current state. A long and inefficient method might be to write:

[SomeTagName] == 0 ? "Message (or) Address 0"
                   :
[SomeTagName] == 1 ? "Message (or) Address 1"
                   :
[SomeTagName] == 2 ? "Message (or) Address 2" 
                   :
 etc. for 100 or more messages...

There are alternatives:

Option 1

A much easier method is to use a CASE statement in steady state. All statements will execute, but only the result of the statement matching the requested index will be returned.

X = Case([SomeTagName],
{ 0 } "Message (or) Address 0", { 1 } "Message (or) Address 1", { 2 } "Message (or) Address 2", { etc });

Option 2

Another option is somewhat more complicated to create, but has the benefit that the messages are stored in application properties instead of code and therefore can be added to or edited by anyone with the Configuration privilege. It is not necessary to write more code when messages change or new messages are created.

1. Create a set of application properties for each situation:

 Situation0         = Message or Address 0 
 Situation1         = Message or Address 1
 Situation2         = Message or Address 2
   (etc.)

2. Write an expression that uses the Variable function to concatenate the tag value onto the prefix "Situation" to return the appropriate property value.

 Variable(Concat("Situation",[<SomeTagName1>]))

No comparisons or If-Else statements are required. This single line of code can return any message that you have created. For more complicated situations, use Concat to build the appropriate property name or combine several values into one message or address.