Skip to main content

Command groups

The following command groups are the basic constructs of Structured Text and can be flexibly combined and nested.

Boolean operations​

Boolean operations are used for binary linking of variables.

| Symbol | Logical operation | Example | | --- | | --- | |NOT | Binary negation | a := NOT b;| | AND| Logical AND | a := b AND c;| | OR | Logical OR | a := b OR c;| | XOR| Exclusive OR | a := b XOR c;|

The truth table for Boolean operations looks like this:

InputANDOR XOR
0 0000
0 1011
1 0011
1 1110

Arithmetic operations​

The Structured Text contains basic arithmetic operations for use. The priorities are to be observed during execution.

SymbolArithmetic operationExample
:= Allocationa := b;
+Additiona := b + c;
-Subtractiona := b - c;
*Multiplicationa := b * c;
/Divisiona := b / c;
MODModulo, integer remainder of division a := b MOD c;

The data type of variables and values is always crucial for their calculation.

| Expression / Syntax | Var1 | Var2 | Var3 | Result | | --- | --- | --- | -- | | Var 1 := 10 / 4; | INT | INT | 2 | | Var1 := 10 / 4; | REAL | INT | 2.0 | | Var1 := 10.0 / 4; | REAL | REAL | INT | 2.5 |

Comparison operators​

Symbol Comparison expressionExample 
=AnonIF a = b THEN
<>UnequalIF a <> b THEN 
>Larger thanIF a > b THEN 
>=Greater than or equal toIF a >= b THEN
<Smaller thanIF a < b THEN
<=Less than or equal toIF a <= b THEN

Decisions​

IF a > b THEN // 1. Comparison
c := 1; // statement if 1. Comparison TRUE
ELSIF a > d THEN // 2. Comparison
e := 1; // statement if 2. Comparison TRUE
ELSE // Alternative branch, no comparison TRUE
f := 1; // Statement of the alternative branch
END_IF // End of the decision

IF - ELSE statement​

IF - ELSIF instruction​

CASE instruction​

The CASE statement is used to group multiple conditional statements with the same conditional variable.

CASE newCase OF // start of Case         
1,5: // for 1 and 5
StateDescription := "Stopped";
2: // for 2
Statedescription := "Running";
3, 4, 6 ... 8: // for 3, 4, 6, 7, 8
Statedescription := "Failed";
ELSE // Alternative branch
(* .. *)
END_CASE // End of Case

In a program cycle, only one step of the CASE instruction is processed at a time. The step variable must be an integer data type.

Grinding​

Loops are processed repeatedly within a cycle. The code is executed until a defined termination condition is met.

To avoid infinite loops, a way should always be provided to end the loop after a certain number of repetitions.

Header controlled loops (FOR, WHILE) check the termination condition before the run, footer controlled loops (REPEAT) at the end.

FOR statement​

The FOR instruction is used to execute a certain number of repetitions of a program part.

Sum := 0;
FOR Index := 0 TO 3 DO
Sum := Sum + Values[ Index ];
END_FOR;

WHILE statement​

The WHILE statement does not have a loop counter. This is called until a condition or expression is FALSE.

WHILE Index < 10 DO
Sum := Sum + Values[ Index ];
Index := Index + 1;
END_WHILE;

REPEAT instruction​

The termination condition is checked in the REPEAT loop only after execution.

Index := 0;
Sum := 0;
REPEAT
Sum := Sum + Values[ Index ];
Index := Index + 1;
UNTIL Index >= 10 END_REPEAT;

EXIT statement​

Can be used with all loop types and results in immediate termination.

REPEAT
IF Exit = TRUE THEN
EXIT;
END_IF
UNTIL Index >5
END_REPEAT