Command groups
The following command groups are the basic constructs of Structured Text and can be flexibly combined and nested.
Boolean operations
Section titled “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
Section titled “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; |
Comparison operators
Section titled “Comparison operators”Comparison operators are used to compate two values. The result is a Boolean value.
| Symbol | Comparison expression | Example |
|---|---|---|
= | equal | 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
Section titled “Decisions”IF statement
Section titled “IF statement”The IF statement is used to make decisions based on a condition. The ELSE branch is optional.
IF a > b THEN // 1. Comparisonc := 1; // statement if 1. Comparison TRUEELSIF a > d THEN // 2. Comparisone := 1; // statement if 2. Comparison TRUEELSE // Alternative branch, no comparison TRUEf := 1; // Statement of the alternative branchEND_IF // End of the decisiongraph
A{IF<br>Condition A<br>THEN} -- TRUE --> B["Statement(s) A"]
B --> C(END_IF)
A -- FALSE --> D(ELSE)
D --> E["Statement(s) B"]
E --> C
IF - ELSIF statement
Section titled “IF - ELSIF statement”If statements can be nested, or have multiple ELSEIF branches.
graph
A{IF<br>Condition A<br>THEN} -- TRUE --> B["Statement(s) A"]
B --> C(END_IF)
A -- FALSE --> D{ELSIF<br>Condition B<br>THEN}
D -- TRUE --> E["Statement(s) B"]
E --> C
D -- FALSE --> F{ELSIF<br>Condition C<br>THEN}
F -- TRUE --> G["Statement(s) C"]
G --> C
F -- FALSE --> H(ELSE)
H --> I["Statement(s) D"]
I --> C
CASE instruction
Section titled “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 CaseIn a program cycle, only one step of the CASE instruction is processed at a time. The step variable must be an integer data type.
graph
A{CASE<br>expression<br>OF} --> B[1, 5]
B --- C["Statement(s) A"]
C --> D(END_CASE)
A --> E[2]
E --- F["Statement(s) B"]
F --> D
A --> G[3, 4, 6 .. 8]
G --- H["Statement(s) C"]
H --> D
A --> I(ELSE)
I --- J["Statement(s) D"]
J --> D
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 loop
Section titled “FOR loop”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;flowchart
A(FOR) --> B[Index := StartValue]
B --> C{Index > EndValue}
C -- FALSE --> D["Statement(s)"]
D --> E[Increase Index]
E --> C
C -- TRUE --> F(END_FOR)
WHILE loop
Section titled “WHILE loop”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;flowchart
A(WHILE) --> B{Condition}
B -- TRUE --> C["Statement(s)"]
C --> A
B -- FALSE --> D(END_WHILE)
REPEAT loop
Section titled “REPEAT loop”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;graph
A(REPEAT) --> B["Statement(s)"]
B --> C{UNTIL<br>Condition}
C -- FALSE --> A
C -- TRUE --> D(END_REPEAT)
EXIT loop statement
Section titled “EXIT loop statement”Can be used with all loop types and results in immediate termination.
REPEAT IF Exit = TRUE THEN EXIT; END_IF UNTIL Index >5END_REPEAT