Structured Text - Expressions and Operators
This topic covers the expressions and operators of the Structured Text language.
An expression consists of operands and operators. An operand can be a constant, a variable, function or function block. Operators are defined by the IEC standard. They are listed on the next page. Here are some examples.
Number | Operation | Symbol | Precedence |
---|---|---|---|
1 | Parenthesis | (expression) |
Highest
Lowest |
2 | Function/Function block |
F/FB name (Argument list) Examples: LN(A), MAX(X.Y) |
|
3 | Exponentiation | ** | |
4 | Negation | - | |
5 | Complement | NOT | |
6 | Multiply | * | |
7 | Divide | / | |
8 | Modulo | MOD | |
9 | Add | + | |
10 | Subtract | - | |
11 | Comparison | <,>,<=,>= | |
12 | Equality | = | |
13 | Inequality | <> | |
14 | Boolean AND | & | |
15 | Boolean AND | AND | |
16 | Boolean Exclusive OR | XOR | |
17 | Boolean OR | OR |
(1) Parenthesis
The formula in the parentheses is calculated first.
Example:
iResult := (2 + 3) * 2;
iResult is 10 because ‘2 + 3’ is calculated first and multiplied by 2.
(2) Function/Function Block
There are predefined functions and function blocks. User functions and user function blocks are also available.
(3) Exponentiation (**)
Example:
rVal := 3;
rResult := 10 ** rVal;
rResult is 1000.
Note: XEM CPU modules support the exponentiation only with the REAL and LREAL data types. If you want to execute the exponentiation with the INT data type for instance, please convert the data type like this.
iVal := 3;
iResult := REAL_TO_INT(10 ** INT_TO_REAL(iVal));
iResult is 1000.
(4) Negation (-)
Invert the sign flag.
Example:
iVal:= 3;
iResult := - iVal;
iResult is -3.
(5) Complement (NOT)
Invert each bit in the data.
Example 1:
xVal := 1;
xResult := NOT xVal;
xResult is 0.
Example 2:
wVal := 2#1100001100111100;
wResult := NOT wVal;
wResult is 2#0011110011000011.
(6) Multiply (*)
Example:
iVal1 := 6;
iVal2 := 7;
iResult := iVal1 * iVal2;
iResult is 42.
(7) Divide (/)
Example:
iVal1 := 56;
iVal2 := 7;
iResult := iVal1 / iVal2;
iResult is 8.
(8) Modulo (MOD)
Example:
iVal1 := 34;
iVal2 := 6;
iResult := iVal1 MOD iVal2;
iResult is 4.
(9) Add (+)
Example:
iVal1 := 2;
iVal2 := 5;
iResult := iVal1 + iVal2;
iResult is 7.
(10) Subtract (-)
Example:
iVal1 := 9;
iVal2 := 3;
iResult := iVal1 - iVal2;
iResult is 6.
(11) Comparison (<,>,<=,>=)
Compare two values and generate a Boolean value.
Example:
iVal1:= 2;
iVal1:= 3;
xResult1 := iVal1 < iVal2;
xResult2 := iVal1 > iVal2;
xResult3 := iVal1 <= iVal2;
xResult4 := iVal1 >= iVal2;
xResult1 is 1.
xResult2 is 0.
xResult3 is 1.
xResult4 is 0.
(12) Equality (=)
Compare two values. If they are the same, generate Boolean value 1. If not, generate Boolean value 0.
Example 1:
iVal1:= 2;
iVal1:= 2;
xResult := iVal1 = iVal2;
xResult is 1.
Example 2:
iVal1:= 2;
iVal1:= 3;
xResult := iVal1 = iVal2;
xResult is 0.
(13) Inequality (<>)
Compare two values. If they are not the same, generate Boolean value 1. If they are the same, generate Boolean value 0.
Example 1:
iVal1:= 2;
iVal1:= 3;
xResult := iVal1 <> iVal2;
xResult is 1.
Example 2:
iVal1:= 2;
iVal1:= 2;
xResult := iVal1 <> iVal2;
xResult is 0.
(14,15) Boolean AND (&, AND)
Evaluate two Boolean values. If the both values are 1, generate Boolean value 1. If not, generate Boolean value 0.
Example:
xResult := xVal1 & xVal2;
or
xResult := xVal1 AND xVal2;
xVal1 | xVal2 | xResult |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
(16) Boolean Exclusive OR (XOR)
Evaluate two Boolean values. If these values are different, generate Boolean value 1. If not, generate Boolean value 0.
Example:
xResult := xVal1 XOR xVal2;
xVal1 | xVal2 | xResult |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
(17) Boolean OR (OR)
Evaluate two Boolean values. If one of these values is 1, generate Boolean value 1. If not, generate Boolean value 0.
Example:
xResult := xVal1 OR xVal2;
xVal1 | xVal2 | xResult |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
LP304B