-

ST Code Objects

This topic describes the following code objects that can be used when programing ST:

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.

OperatorExampleValue of exampleDescriptionPriority
( )(2+3)* (4+5)45ParenthesizationHighest
Function name (parameter list)MAX(4,10)10Function call1
-

NOT
-10

NOT TRUE
-10

false
Negation

Complement
1
**3.0**481.0Exponentiation1
*

/

MOD
10*3

6/2

17 MOD 10
30

3

7
Multiplication

Division

Modulo
1
+

-
2+3

4-2
5

2
Addition

Subtraction
1
<, >, <=, >=4 > 12falseComparison1
=

<>
T#26h = T#1d2h

8 <>16
true

true
Equality

Inequality
1
&, ANDTRUE & FALSEfalseBoolean AND1
XORTRUE XOR FALSEtrueBoolean exclusive OR1
ORTRUE OR FALSEtrueBoolean ORLowest

Statements

Note
Depending on the used target, the nesting level of statements may be limited. Furthermore, there may exist some other controller-specific rules.

KeywordExampleDescription
:=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.
RETURNRETURN;Return statement

The return statement exits the called function, function block or program and returns to the calling POU.
IFIF 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.
CASECASE 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.
FORFOR 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.

Note
If 'BY' is not specified, the default value '1' is used. All values must then be INT data types.

WHILEWHILE 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.
REPEATREPEAT
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.
EXITFOR 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.