Skip to main content

ST Statement: REPEAT

The REPEAT loop is used like the WHILE loop, but with the difference that CODESYS only checks the abort condition after the execution of the loop. The consequence of this behavior is that the REPEAT loop is executed at least once, regardless of the abort condition.

Syntax:

REPEAT
  <instructions>
UNTIL <boolean expression>
END_REPEAT;

CODESYS executes the <instructions> statements until the <Boolean expression> returns TRUE.

If the Boolean expression already returns TRUE at the first evaluation, CODESYS executes the statements precisely one time. If the Boolean expression never applies the value TRUE, then the statements are repeated endlessly, as a result of which a runtime error results.

Example 74. Example
REPEAT
 Var1 := Var1*2;
 iCounter := iCounter-1;
UNTIL
 iCounter = 0
END_REPEAT;


In a certain sense the WHILE and REPEAT loops are more powerful than the FOR loop, since the number of executions of the loop doesn't already need to be known before its execution. In some cases you can only work with these two kinds of loop. However, if the number of loop executions is clear, then a FOR loop is preferable in order to avoid infinite loops.

As an extension to the IEC 61131-3 standard you can use the CONTINUE statement within the WHILE loop.