IfElse
Description: |
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 |
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 (typically an Execute statement) which is executed when Condition is true. |
FALSECase |
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" : 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, i++), { 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.