REPEAT loop

Syntax

REPEAT
    <statement_list>
UNTIL <bool_expr>
END_REPEAT;

Note no ; after <bool_expr> in the IEC standard — END_REPEAT follows directly. matiec accepts both forms.

Semantics

  1. Run the body.
  2. Evaluate <bool_expr>. If TRUE, exit. If FALSE, go to step 1.

The body always runs at least once, in contrast with WHILE. Use REPEAT when you need to do something at least once before checking whether to continue.

REPEAT vs. WHILE

QuestionUse
“Run while X is true (might run zero times)”WHILE x DO ... END_WHILE
“Run until X becomes true (always at least once)”REPEAT ... UNTIL x END_REPEAT

The condition direction is flipped: WHILE continues while the condition is TRUE; REPEAT continues until the condition becomes TRUE.

Example

(* Read until end-of-data marker *)
REPEAT
    iValue := ReadNext();
    arr[iIndex] := iValue;
    iIndex := iIndex + 1;
UNTIL iValue = SENTINEL
END_REPEAT;

The same scan-cycle warning as WHILE applies — bound the loop with a safety counter if the exit condition depends on external data.

IEC reference

IEC 61131-3 third edition (2013), clause 6.6.3.6.

matiec conformance

Implemented per the standard.