ST instruction REPEAT

You use the REPEAT loop in the same way as the WHILE loop, but with the difference that TwinCAT does not check the termination condition until after the loop has been executed. This behavior has the consequence that the REPEAT loop will run at least once, no matter what the terminating condition is.

Syntax:

REPEAT
    <instructions>
UNTIL <boolean expression>
END_REPEAT;

TwinCAT executes the <instructions> repeatedly until the <boolean expression> returns TRUE.

If the Boolean expression is TRUE at the first evaluation, then TwinCAT executes the instructions once. If the Boolean expression never takes the value TRUE, the instructions are repeated continuously, causing a runtime error.

Example:

REPEAT
    nVar1 := nVar1*2;
    nCounter := nCounter-1;
UNTIL
    nCounter = 0
END_REPEAT;

The WHILE and REPEAT loops are, in a sense, more powerful than the FOR loop, since you do not need to know the number of iterations before you run the loop. In some cases, you can work only with these two types of loops. However, if the number of iterations is known, a FOR loop is preferable to avoid infinite loops.

In addition to the IEC 61131-3 standard, you can use the CONTINUE instruction within the WHILE loop.

See also: