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>
until the <boolean expression>
returns TRUE
.
If the boolean expression already returns TRUE
at the first evaluation, CODESYS executes the instructions precisely once. If the boolean expression never adopts
the value TRUE
, then the instructions are repeated endlessly, as a result of which a runtime error
results.
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.
If the number of executions of the loop is clear, however, then a FOR
loop is preferable in order to avoid endless loops.
As an extension to the IEC 61131-3 standard you can use the CONTINUE instruction within the WHILE
loop.