Explainer
How a REPEAT loop runs in PLC structured text
Short answer
In IEC 61131-3 structured text, a REPEAT loop executes its body first and evaluates the UNTIL condition after it, so the body always runs at least once and the whole loop completes inside one task invocation. With i := 0 and UNTIL i >= 10, the body runs 10 times and i ends at exactly 10; the test position is what explains the off-by-one cases and when WHILE fits better.
On this page
- The execution order: body first, test after
- The worked example: 0 to 10 in ten passes
- The at-least-once rule when the condition already holds
- The boundary: >= ends at 10, > ends at 11
- What the loop does to the scan
- Reset every scan or count across scans
- Choosing between FOR, WHILE and REPEAT
- Questions asked
A REPEAT loop in IEC 61131-3 structured text executes its body first and evaluates the UNTIL condition after it, so the body always runs at least once, and the whole loop — initialization, iterations, exit — completes inside one task invocation. For the textbook counter i := 0; REPEAT i := i + 1; UNTIL i >= 10 END_REPEAT, the body runs 10 times and i ends at exactly 10. That execution order answers every practical question about these routines: why the body runs even when the condition is already satisfied, when to switch to WHILE, why the loop cannot wait on a field device, and why the counter walks back to 0 every scan unless you move its initialization.
The execution order: body first, test after #
REPEAT is a structured text construct, and where ST fits among the IEC 61131-3 languages is covered in which IEC 61131-3 language to use for each control task. The standard defines REPEAT...UNTIL as a post-test loop: the statements between REPEAT and UNTIL execute once, then the Boolean expression after UNTIL is evaluated. FALSE sends execution back to REPEAT; TRUE continues after END_REPEAT. WHILE is the pre-test counterpart — it evaluates its condition before the body and runs the body while that condition holds — so converting between the two means inverting the condition as well as moving it.
The loop variable is ordinary memory. Nothing about REPEAT resets it, preserves it, or scopes it; it holds whatever value was last written to it, which is why the i := 0 line in front of the loop is doing real work.
The worked example: 0 to 10 in ten passes #
Every name below is an illustrative variable, not a device-specific parameter:
VAR
i : INT := 0; // loop counter, reset before each use
hits : INT := 0; // accumulator that proves the pass count
END_VAR
i := 0;
hits := 0;
REPEAT
i := i + 1; // read i, add 1, write it back
hits := hits + 1;
UNTIL i >= 10
END_REPEAT;
Pass by pass:
| Pass | i at entry | i after increment | i >= 10 | Path taken |
|---|---|---|---|---|
| 1 | 0 | 1 | FALSE | back to REPEAT |
| 9 | 8 | 9 | FALSE | back to REPEAT |
| 10 | 9 | 10 | TRUE | after END_REPEAT |
With an increment of 1 and the test written i >= limit, the pass count is limit minus start: 10 - 0 = 10. The accumulator gains 10, and i holds 10 after END_REPEAT. The test that proves the result: run one task invocation, then read both variables online — hits = 10 and i = 10. If hits reads 9, the start value was 1 or the operator is > rather than >=.
The at-least-once rule when the condition already holds #
Delete the i := 0 line and suppose i still holds 50 from an earlier calculation. The loop still runs the body once: i becomes 51, the test 51 >= 10 returns TRUE, and execution leaves the loop with i at 51. The body executed even though the condition was satisfied on entry — that is the post-test definition at work, not a fault.
Decision: when zero passes must be possible — averaging over a batch length that can be 0, a retry that may not be needed — use WHILE and test before the body. Keep REPEAT where one pass is always correct: stepping an index you always advance at least once, or processing a buffer that always holds at least one element.
The boundary: >= ends at 10, > ends at 11 #
The >= operator includes the boundary, and the increment happens before the test, so this loop exits exactly on 10. Write the exit as UNTIL i > 10 and the comparison first returns TRUE at 11: the body runs 11 passes (11 - 0 = 11) and i ends at 11. To stop at exactly 10, use >= 10; to stop strictly past 10, use > 10 and accept 11. When a routine finishes one high, read the comparison operator before touching the increment.
What the loop does to the scan #
The loop runs inside the task that calls it, start to finish, before any later logic in that task executes. Ten integer passes are trivial next to the rest of a scan; the limit that matters is the task watchdog, the maximum execution time configured on the task in the controller. A loop whose UNTIL expression never becomes TRUE holds the task inside the loop until that watchdog expires and the controller faults the task.
The expression that never becomes TRUE is the one that depends on something outside the loop. The process input image is refreshed when the task is invoked, not while it runs, so an exit written as UNTIL LimitSwitchClosed never fires — the input cannot change under the loop. Communication responses and operator actions fail the same way, because the logic that services them sits after the loop and never executes. The rule: the loop body itself, or a call the body makes, must assign every variable in the UNTIL expression. Anything else belongs in state that advances once per scan, the per-scan execution model that also explains why a CODESYS TON output stays on after its rung goes false.
One boundary you never cross with a loop: if the exit condition guards anything touching personnel — a door, a light curtain, a stop — that function belongs in the safety program, not in standard structured text, and the handoff is the one described for mapping standard PLC tags into a safety program.
Reset every scan or count across scans #
The task re-executes on every invocation, so an unconditional i := 0 in front of the loop makes it a per-scan calculation: 0 to 10 on every scan. That is what you want for work redone each cycle — scaling an array of channels, rebuilding a checksum.
When the counter must persist — parts counted across scans, a sequence walked one step per scan — the increment and the test move out of the loop to the top level of the task, with Step declared as the same INT alongside the data that persists:
// one step per scan, not ten passes in one scan
IF Step < 10 THEN
Step := Step + 1;
END_IF;
Ten scans walk Step from 0 to 10, and the rest of the program runs between steps. Diagnose which structure you are reading by watching the variable at the end of consecutive scans: 10, then 10, then 10 is the per-scan loop; 1, then 2, then 3 is the per-scan step. An unexpected value at the start of a scan means another statement is writing the location — trace every write to it before changing the loop.
Choosing between FOR, WHILE and REPEAT #
- The number of passes is known before the loop starts: FOR.
FOR Index := 0 TO 9 BY 1 DO ... END_FORcarries start, end and step in the header, and the increment cannot be forgotten. - Zero passes must be possible: WHILE, condition tested first, inverted relative to the UNTIL expression.
- At least one pass is always required: REPEAT, condition tested last.
Questions asked
How many passes does a REPEAT loop make for a given start and limit?
With an increment of 1 and the test written as i >= limit, the body runs limit minus start passes: from 0 to 10 that is 10 passes, and with a start of 1 it is 9. If the test is i > limit, add one pass, because the comparison first returns TRUE one increment later.
Why does my counter finish at 11 instead of 10?
The exit test uses > rather than >=. With the increment before the test and UNTIL i > 10, the comparison first returns TRUE when i reaches 11, so the body runs 11 passes. Change the operator to >= to stop at exactly 10, or lower the constant to 9 if you keep the strict greater-than.
Can I wait inside a REPEAT until a field input changes?
No. The input image is fixed for the whole task invocation, so the exit cannot fire and the task stalls until its watchdog expires. Hold the state in a variable and advance it once per scan instead, with the input read fresh each invocation.
Why does my loop variable go back to 0 every scan?
The initialization runs unconditionally ahead of the loop, and the task re-executes it on every invocation, so the loop completes its 0 to 10 walk each scan. That is correct for per-scan calculations. For a value that persists, remove the in-scan initialization and increment once per scan at the top level of the task.
When is FOR or WHILE the better choice than REPEAT?
Use FOR when the pass count is known in advance: the start, end and step sit in the header and the increment cannot be forgotten. Use WHILE when zero passes must be possible, because it tests before the body. REPEAT is right only when at least one pass is always required.