BREAK is the "leave the loop" instruction (available on FX5U and similar platforms): the FOR~NEXT loop ends early when BREAK executes, skipping the remaining iterations. It answers one problem — a loop exists to find something, and once found, spinning through the rest is waste. Used in the right places it saves scan time; used wrongly it leaves half-finished loops behind. The sections below cover uses, coding, pairing and cautions.
| Use | Scene | Without it |
|---|---|---|
| Exit on found | Searching an array for a value (e.g. the current recipe number among 100) | Remaining iterations spin for nothing after the hit |
| Stop at first anomaly | Scanning alarm words, recording the position of the first non-zero | Later checks overwrite earlier records — only the last survives |
| Early end on condition change | Batch-transferring data, stopping when the destination nears full | The overflow area gets corrupted |
The shared pattern: the loop body contains a check, and once it passes "the job is done" — the remaining iterations are meaningless. That is BREAK's portrait.
Ladder equivalents: a comparison contact between FOR and NEXT; on equality, store the index register into the result register and execute BREAK. Note that BREAK exits only its own FOR layer — with nested loops each layer needs its own BREAK (or use a flag checked by every layer to exit all at once).
Any search using BREAK needs a "found?" flag: clear it before the loop, set it on the hit together with BREAK; after the loop, branch on the flag — when not found, the result register holds garbage, and using it directly is a hidden hazard. The canonical triplet: initialise result and flag → search (set flag, store result, BREAK on hit) → branch on the flag at the exit. Missing any of the three produces the classic "sometimes right, sometimes wrong" disease.
| Method | Coding | Fits |
|---|---|---|
| BREAK instruction | Exit the current layer directly on the hit | Single-layer loops, when the platform supports it |
| Flag-based exit | Loop condition ANDed with "NOT found yet", checked per layer | Multi-layer nests, older platforms without BREAK |
The flag method reads clearer in deep nests: every FOR's condition carries "stop entering once found" and the layers unwind naturally. BREAK is shorter code, the flag method more universal — pick one for the team and stay with it.
Loop cost ≈ iterations × instructions per iteration. A 100-iteration search hitting on average at the 30th saves 70 iterations with BREAK; at 10 instructions per iteration and a 10 ms scan budget, the saving matters in frequently called search subroutines. Conversely, few iterations (≤10) or loops that must always run to completion (bulk assignment) gain nothing — bulk assignment belongs to FMOV/FILL block instructions, not loops.
| Symptom | Cause | Fix |
|---|---|---|
| Hit found but loop keeps running | BREAK outside the loop body / condition incomplete | Verify BREAK placement and the condition |
| Nested loops do not all exit | BREAK exits one layer only | Break per layer or switch to the flag method |
| Garbage result when not found | No validity flag | Add the triplet: init + flag + exit branching |
| Result index uncertain | Relying on the loop variable as the result | Store the result explicitly on the hit |
| Scan time worse, not better | Break check added to a loop that always completes | Assess hit probability; drop BREAK on full-run loops |
BREAK's essence is shifting the loop from "iteration-driven" to "task-driven": once the job is done, do not spend the remaining effort. The one-line test — does the loop body contain "half-done counts as done" semantics? If yes, BREAK fits; loops that must run to completion (initialisation, bulk transfer) take block instructions or plain loops.
BREAK support and behaviour follow the PLC's programming manual (e.g. Mitsubishi FX5U Programming Manual, applied instructions, FOR/NEXT and BREAK); CODESYS/TwinCAT use EXIT (IEC 61131-3) for the same semantics; loop impact on the scan cycle is governed by each platform's watchdog settings.
Before shipping any search loop, ask three questions: is there a validity flag? do nested layers exit together? has worst-case time been estimated? Three yeses and it may run.