Iteration is a core concept in programming, and Structured Text (ST) provides a variety of loop types to implement it. However, when working with TwinCAT, understanding the behavior of loops—especially the FOR-loop—can be trickier than it seems. This article offers a concise introduction to TwinCAT loops and their peculiarities.
FOR-Loop
The FOR-loop is an entry-controlled loop that executes statements repeatedly until the <counter> meets the specified end condition:
Without a
<step size expression>: Loop stops when the <counter> is greater than the <end condition expression>.With a positive <step size expression>: Loop stops when the <counter> exceeds the <end condition expression>.
With a negative <step size expression>: Loop stops when the <counter> is below the <end condition expression>.
The following components must be or return an integer:
<counter>, <initial expression>, <end condition expression>, and <step size expression>:
ANY_BIT:BYTE,WORD,DWORD,LWORD,__XWORDANY_INT:SINT,USINT,INT,UINT,DINT,UDINT,LINT,ULINT,__XINT, or__UXINT
After each execution of the loop’s <instructions>, the <counter> is automatically incremented by the value of the <step size expression>. If no step size is specified, the default step size is 1.
The <end condition expression> must not match the upper or lower limits of the <counter> data type, as this could result in an endless loop due to overflow.
The EXIT-statement is used to jump out of a loop.
The CONTINUE-statement interrupts the current iteration and proceeds with the next iteration of the loop.
Syntax
FOR <counter> := <initialization expression> TO <end condition expression> [BY <step size expression>] DO
<instruction>
[CONTINUE;]
[EXIT;]
[<instructions>]END_FOR
The parts in square brackets are optional.
Flowchart
The <step size expression> is evaluated three times per loop cycle because of a fully evaluated OR. The <end condition expression> is evaluated once per cycle without <step size expression> and twice per cycle with <step size expression>.
Examples
Simple Examples
Example with Pointer Operations
Example with EXIT and CONTINUE
Example with Complex Expressions
WHILE-Loop
The WHILE-loop is an entry-controlled loop that executes statements repeatedly until the <condition expression> returns FALSE.
The EXIT-statement is used to jump out of a loop.
The CONTINUE-statement interrupts the current iteration and proceeds with the next iteration of the loop.
Syntax
WHILE <condition expression> D
<instruction>
[CONTINUE;]
[EXIT;]
[<instructions>]END_WHILE
The parts in square brackets are optional.
Flowchart
Examples
Simple Example
Example with CONTINUE and EXIT
REPEAT-Loop
The REPEAT -loop is a variant of the WHILE-loop. This loop will execute the code block once, before checking if the <condition expression> is TRUE. Then it will repeat the loop as long as the <condition expression> returns FALSE.
The EXIT-statement is used to jump out of a loop.
The CONTINUE-statement interrupts the current iteration and proceeds with the next iteration of the loop.
Syntax
REPEAT
<instruction>
[CONTINUE;]
[EXIT;]
[<instructions>]UNTIL
<condition expression>END_REPEAT
The parts in square brackets are optional.
Flowchart
Examples
Simple Example
Example with CONTINUE and EXIT
For more insights on this topic, check out our in-depth video on loops in TwinCAT on YouTube here.