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 compare 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_REPEATCONTINUE loop statement
Section titled “CONTINUE loop statement”CONTINUE skips the rest of the current loop iteration and proceeds with the
next one. Like EXIT, it can be used in any loop type.
FOR Index := 0 TO 9 DO IF Values[Index] = 0 THEN CONTINUE; // skip zero entries END_IF Sum := Sum + Values[Index];END_FOR;Execution control
Section titled “Execution control”RETURN statement
Section titled “RETURN statement”RETURN exits the current POU (program, function, function block body or method)
immediately. In a function or method that has a return value, set the return
variable before returning.
IF NOT enabled THEN RETURN; // leave the body earlyEND_IF// ... rest of the body only runs when enabledMethod invocation statement
Section titled “Method invocation statement”A method on a function-block or class instance can be called as a standalone
statement (without using its result). Use dot notation on the instance, or
THIS. / SUPER. inside a method.
motor.Start();counter.Increment(by := 1);SUPER.Init();See Methods, classes and interfaces for the full object-oriented model.