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:
| Input | AND | OR | XOR |
|---|---|---|---|
| 0 0 | 0 | 0 | 0 |
| 0 1 | 0 | 1 | 1 |
| 1 0 | 0 | 1 | 1 |
| 1 1 | 1 | 1 | 0 |
Arithmetic operations
The Structured Text contains basic arithmetic operations for use. The priorities are to be observed during execution.
| Symbol | Arithmetic operation | Example |
|---|---|---|
:= | Allocation | a := b; |
+ | Addition | a := b + c; |
- | Subtraction | a := b - c; |
* | Multiplication | a := b * c; |
/ | Division | a := b / c; |
MOD | Modulo, 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 expression | Example | |
|---|---|---|---|
= | Anon | IF a = b THEN | |
<> | Unequal | IF a <> b THEN | |
> | Larger than | IF a > b THEN | |
>= | Greater than or equal to | IF a >= b THEN | |
< | Smaller than | IF a < b THEN | |
<= | Less than or equal to | IF 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