MOULDLKnowledge BaseElectrical Knowledge Base3D Model DownloadsMembership
中文版Model Store »
Knowledge Base › Electrical Knowledge Base › When should you use the BREAK instruction?
HomeModels
Table of contents

On this page

PLC & Programming (100 articles)

Quick Reference

  • PLC & Programming
  • Electrical & Electronic Basics (monthly)(每月更新中)
  • Electrical Control & Drawings (monthly)(每月更新中)
  • Drives & Actuators (monthly)(每月更新中)
  • Sensors & Detection (monthly)(每月更新中)
  • Industrial Communication & Networks (monthly)(每月更新中)
  • HMI & Host Systems (monthly)(每月更新中)
  • Industrial Robots & Motion Control (monthly)(每月更新中)
  • Mechatronic System Integration (monthly)(每月更新中)
  • Fault Diagnosis & Standards (monthly)(每月更新中)

When should you use the BREAK instruction?

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.

1. Three typical uses of BREAK

UseSceneWithout it
Exit on foundSearching an array for a value (e.g. the current recipe number among 100)Remaining iterations spin for nothing after the hit
Stop at first anomalyScanning alarm words, recording the position of the first non-zeroLater checks overwrite earlier records — only the last survives
Early end on condition changeBatch-transferring data, stopping when the destination nears fullThe 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.

2. A typical coding: table lookup

FOR i := 1 TO 100 DO
  IF D[i] = target THEN
    result := i;
    BREAK;
  END_IF;
END_FOR;

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).

3. What if it is not found: the validity flag

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.

4. BREAK versus the flag method

MethodCodingFits
BREAK instructionExit the current layer directly on the hitSingle-layer loops, when the platform supports it
Flag-based exitLoop condition ANDed with "NOT found yet", checked per layerMulti-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.

5. Estimating the scan-time saving

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.

6. Common errors

SymptomCauseFix
Hit found but loop keeps runningBREAK outside the loop body / condition incompleteVerify BREAK placement and the condition
Nested loops do not all exitBREAK exits one layer onlyBreak per layer or switch to the flag method
Garbage result when not foundNo validity flagAdd the triplet: init + flag + exit branching
Result index uncertainRelying on the loop variable as the resultStore the result explicitly on the hit
Scan time worse, not betterBreak check added to a loop that always completesAssess hit probability; drop BREAK on full-run loops

7. Notes

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.

8. References

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.

9. Cautions

⚠️ Three traps to avoid:
① One BREAK in a multi-layer nest — the inner layer exits, the outer keeps running, nothing improved;
② Using the result register without checking the found-flag — occasionally "finding a recipe that does not exist";
③ A huge loop ceiling without BREAK — one branch overruns the scan and trips the watchdog.

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.

Related reading

Related 3D models: Ready to build it? Download free 3D models & CAD drawings at MOULDL »
MOULDL · Electrical Knowledge Base · free self-study reference for electrical & automation engineers, 3D CAD models at MOULDL