ST Code Objects
This topic describes the following code objects that can be used when programing ST:- Expressions
- Operands and operators
- Statements (assignment, iteration, selection, etc.)
Expressions
Expressions are parts of statements. An expression returns exactly one value which is then used for the execution of the statement where the expression is used.
Each expression consists of
operands and operators (see below) or functions calls.
Operands and operators
Operands and operators are both parts of ST expressions.
Operands can be literals, variables or names of functions. Operands are logically combined using operators , i.e., operators are applied to operands.
In an expression, the operator with the highest priority is executed first, followed by the operator with the next lower priority. The following ST operators are defined.
Operator | Example | Value of example | Description | Priority |
---|---|---|---|---|
( ) | (2+3)* (4+5) | 45 | Parenthesization | Highest |
Function name (parameter list) | MAX(4,10) | 10 | Function call | |
-NOT | -10 NOT TRUE | -10false | NegationComplement | |
** | 3.0**4 | 81.0 | Exponentiation | |
*/MOD | 10*36/217 MOD 10 | 3037 | MultiplicationDivisionModulo | |
+- | 2+34-2 | 52 | AdditionSubtraction | |
<, >, <=, >= | 4 > 12 | false | Comparison | |
=<> | T#26h = T#1d2h8 <>16 | truetrue | EqualityInequality | |
&, AND | TRUE & FALSE | false | Boolean AND | |
XOR | TRUE XOR FALSE | true | Boolean exclusive OR | |
OR | TRUE OR FALSE | true | Boolean OR | Lowest |
Statements
Note
Depending on the used target, the nesting level of statements may be limited. Furthermore, there may exist some other controller-specific rules. |
Keyword | Example | Description | |
---|---|---|---|
:= | variableName := expression; | Assignment statement The simplest ST statement type. It copies the value of the expression on the right to the variable on the left. Both the variable on the left and the value of the expression on the right must have the same data type. Otherwise, a type conversion must be used before. | |
RETURN | RETURN; | Return statement The return statement exits the called function, function block or program and returns to the calling POU. | |
IF | IF a < b THEN c:=1; ELSIF a=b THEN c:=2; ELSE c:=3; END_IF; | Selection statement A group of statements is executed only if the associated Boolean expression 'a<b' is TRUE. If the condition is FALSE, either no statement is executed or the group of statements following ELSE is executed. | |
CASE | CASE f OF 1: a:=3; 2..5: a:=4; 6: a:=2; b:=1; ELSE a:=0; END_CASE; | Selection statement A group of statements is executed according to the value of the expression following the keyword CASE. The variable or expression 'f' must be of the data type ANY_INT. | |
FOR | FOR a:=1 TO 10 BY 3 DO f[a] :=b; END_FOR; | Iteration statement
A group of statements is executed repeatedly increasing the variable 'a' by 3, beginning at '1' and finishing at '10'. The starting point is indicated by the assigned value of the control variable 'a'. The final value is indicated following 'TO' and the increments are indicated following 'BY'. All values must be of the same ANY_INT data type.
|
|
WHILE | WHILE b > 1 DO b:= b/2; END_WHILE; | Iteration statement A group of statements is executed repeatedly until the associated Boolean expression 'b>1' is FALSE. The statement's exit condition is evaluated at the beginning of the loop. If the condition is FALSE, the loop is not executed. | |
REPEAT | REPEAT a := a*b; UNTIL a < 10000 END_REPEAT; | Iteration statement A group of statements is executed repeatedly until the associated Boolean expression 'a<10000' is TRUE. The statement's exit condition is evaluated at the end of the loop. If the condition is FALSE, the loop is executed at least once. | |
EXIT | FOR a:=1 TO 2 DO IF flag THEN EXIT; END_IF SUM:= SUM + a END_FOR | Exit statement The exit statement can be used to abort the execution of an iteration statement. |