ST Expressions
An expression is a construct that returns a value after it was evaluated.
Expressions are composed of operators and operands. An operand can be a constant, a variable, a function call or another expression.
Examples:
2014 | (* constant *) |
nVar | (* variable *) |
F_Fct (a, b) | (* function call *) |
(x*y)/z | (* expression *) |
See also:
Evaluation of Expressions
The evaluation of an expression is carried out by processing the operators according to certain binding rules. TwinCAT first processes the operator with the strongest binding. Operators with the same binding strength are processed from left to right.
Operation | Symbol | Binding strength |
---|---|---|
Put in parentheses | (Expression) | Strongest binding |
Function call | Function name (parameter list) all operators with syntax: <operator> () |
|
Exponentiation | EXPT |
|
Negate Build. complements | - NOT |
|
Multiply Divide Modulo | * / MOD |
|
Add Subtract | + - |
|
Compare | <,>,<=,>= |
|
Equal to Not Equal to | = <> |
|
Bool AND | AND |
|
Bool XOR Bool OR | XOR OR | Weakest binding |
Sample:
In the following sample the operators AND_THEN and OR are used. Note that OR has the weakest binding and that TwinCAT executes expressions at other operands of the AND_THEN operator only if the first operand of the AND_THEN operator is TRUE.
Therefore, the results for the four expressions shown here are as follows:
- The pointer "pSample" is dereferenced for none of the four expressions. The background is that the AND_THEN operator is used, and dereferencing would take place at the other operands of the AND_THEN operator. However, since the first operand of the AND_THEN operator already returns FALSE (since "pSample" has the value 0), the further AND_THEN operands and thus the pointer dereferences are not executed.
Using the AND_THEN operator avoids a null pointer exception at runtime. If the AND operator was used instead of the AND_THEN operator, the pointer "pSample" would be dereferenced as a null pointer within the operands, which would result in a null pointer exception. - In expressions 1 and 2, OR follows as a further operator after the AND_THEN operations. Therefore, the counters "nCounter1" and "nCounter2" increment if "pSample" is 0 and "bTest" is TRUE. The abbreviation of the expressions is "IF <FALSE> OR TRUE THEN", which returns TRUE.
- In expressions 3 and 4, on the other hand, "nCounter3" and "nCounter4" do not increment if "pSample" is 0 and "bTest" is TRUE, since the first operand, "pSample <> 0", returns FALSE. Due to AND_THEN and the parentheses, no further operands or operators are checked. The abbreviated form of the expressions is "IF <FALSE> AND_THEN <...>", which returns FALSE.
PROGRAM MAIN
VAR
pSample : POINTER TO INT;
bTest : BOOL := TRUE;
nCounter1 : INT;
nCounter2 : INT;
nCounter3 : INT;
nCounter4 : INT;
END_VAR
// Expression 1
IF (pSample <> 0) AND_THEN (pSample^ = 250) AND_THEN (pSample^ <> 300) OR bTest THEN
nCounter1 := nCounter1 + 1; // increasing if (pSample = 0) and (bTest = TRUE)
END_IF
// Expression 2
IF ((pSample <> 0) AND_THEN (pSample^ = 250) AND_THEN (pSample^ <> 300)) OR bTest THEN
nCounter2 := nCounter2 + 1; // increasing if (pSample = 0) and (bTest = TRUE)
END_IF
// Expression 3
IF (pSample <> 0) AND_THEN ((pSample^ = 250) AND_THEN (pSample^ <> 300) OR bTest) THEN
nCounter3 := nCounter3 + 1; // not increasing if (pSample = 0) and (bTest = TRUE)
END_IF
// Expression 4
IF (pSample <> 0) AND_THEN ((pSample^ = 250) OR bTest) THEN
nCounter4 := nCounter4 + 1; // not increasing if (pSample = 0) and (bTest = TRUE)
END_IF