Expressions and Parentheses
Structured Text (ST) expressions combine ST operators and variable or constant operands. For each single expression (combining operands with one ST operator), the type of the operands must be the same. This single expression has the same data type as its operands, and can be used in a more complex expression. For example:
Single expression | |
---|---|
(boo_var1 AND boo_var2) | Has BOOL type |
not (boo_var1) | Has BOOL type |
(sin (3.14) + 0.72) | Has DINT type |
(t#1s23 + 78) | Is an invalid expression |
Parentheses are used to isolate sub parts of an expression and to explicitly order the priority of operations. When no parentheses are given for a complex expression, the operation sequence is implicitly given by the default priority between ST operators.
Precedence | Operators | Symbols |
---|---|---|
1 (Highest) | Function evaluation | identifier(arguement list) For example: MAX(X,Y) |
2 | Negation | - |
Complement | NOT | |
3 | Multiplication | * |
Division | / | |
4 | Addition | + |
Subtraction | - | |
5 | Comparison | <, >, <=, >= |
6 | Equality | = |
Inequality | <> | |
7 | Boolean AND | &, AND |
8 | Boolean Exclusive OR | XOR |
9 (Lowest) | Boolean OR | OR |
Examples
Parentheses examples | ||
---|---|---|
2 + 3 * 6 | equals 2+18=20 | Because multiplication operator has a higher priority |
(2 + 3) * 6 | equals 5*6=30 | Priority is given by parenthesis |
Provide Feedback