Topic: DMD0102

Help File Version: 2.9.4.37

IF - If / Else Expression


This instruction can only be used in the Expression parameter of the Calculate Expression (MATH) instruction.

 

The 3-parameter If / Else Expression (IF) math function evaluates the first parameter to determine which of the following two parameters to process. If the first parameter is non-zero (TRUE, ON) the Result will be the value of the second parameter, If the first parameter is zero (FALSE, OFF) the Result will be the value of the third parameter.

 

The parameter list must consist of 3 parameters separated by commas. Each of the parameters is an expression, which can be a constant value, a bit or numeric memory location, an arithmetic expression, and / or mathematical function or other expressions up to a maximum of 1024 characters. Expressions can be nested, so use of parentheses is recommended to ensure proper evaluation order.

 

This function has the following three parameters:

 

  • The first parameter designates the Boolean value or Boolean condition to evaluate. This can be any readable numeric location, or any readable bit location, any constant value, or any other Math expression.
     

  • If the first expression parameter evaluates to a non-zero value (TRUE, ON) the value of the second parameter is used.  The second parameter can be any readable numeric location, or any readable bit location, or any constant value, or any other nested Math expression.
     

  • If the first parameter evaluates to a zero value (FALSE, OFF) the value of the third parameter is used.  The third parameter can be any readable numeric location, or any readable bit location, or any constant value, or any other nested Math expression.


 

In the example above:

if C0 is ON then

D0 will contain the value of D1

else (C0 is OFF)

D0 will contain the value of D2.


If / Else Expression parameters can be nested, meaning that any or all of the three parameters in the If / Else Expression can be If / Else Expressions themselves as shown here:

 

 

In the example above:

if C0 is ON then

If C6 is ON then

D0 will contain the value of D1

else (C6 is OFF)

D0 will contain the value of D2

 

else (C0 is OFF)

If C7 is ON then

D0 will contain the value of D3

else (C7 is OFF)

D0 will contain the value of D4

 


Any or all of the three parameters can consist of expression combinations as show here:

 

 

In this example:

if D1 is greater than D2 then

D0 will contain the value of D1 - D2
 

else D2 is greater than D1

D0 will contain the value of D2 - D1


See Also:

MATH - Calculate Expression

 

IF - If / Else Expression

 

COUNTIF - Count If

 

SUMIF - Sum If

 


Rung Example: