Skip to content

Command groups

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

Boolean operations are used for binary linking of variables.

SymbolLogical operationExample
NOTBinary negationa := NOT b;
ANDLogical ANDa := b AND c;
ORLogical ORa := b OR c;
XORExclusive ORa := b XOR c;

The truth table for Boolean operations looks like this:

InputANDORXOR
0 0000
0 1011
1 0011
1 1110

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 divisiona := b MOD c;

Comparison operators are used to compate two values. The result is a Boolean value.

SymbolComparison expressionExample
=equalIF 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

The IF statement is used to make decisions based on a condition. The ELSE branch is optional.

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
graph 
    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 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

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.

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.

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)

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)

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)

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