DevLearningTools

🚧 This site is under active construction — new tools, guides, and pages are added every week.

MODULE 4 · LESSON 06

break

Exiting a loop immediately in ColdFusion — CFScript's break statement and the <cfbreak> tag, including nested loops and the label attribute for targeting a specific outer loop.

New lessons are added one at a time as the course gets built out — a graded quiz for each lesson is still on the way.

break exits a loop immediately, skipping any remaining iterations — useful once you've found what you were looking for, or once continuing would be pointless or wasteful.

It works inside for, while, and do...while loops in CFScript, and inside any <cfloop> in tag syntax via the <cfbreak> tag.

Learning Objectives

After completing this lesson, you'll be able to:

  • Use break to exit a loop early in CFScript.
  • Use <cfbreak> to exit a loop early in tag syntax.
  • Understand that break only exits the innermost loop by default.
  • Use a label to break out of a specific outer loop from a nested loop.
  • Avoid a subtle gotcha with <cfbreak> inside a switch statement.

Basic break

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

break in a while Loop

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

break Only Exits the Innermost Loop

In a nested loop, a plain break only stops the loop it's directly inside — the outer loop keeps running.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

The inner loop stops early every time col reaches 2, but all three passes of the outer row loop still happen.

Breaking Out of a Specific Outer Loop

To exit an outer loop directly from inside a nested loop, label the outer loop and break that label by name.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

A label is just an identifier followed by a colon, placed right before the loop it names — outerLoop: for (...) in CFScript, or label="outerLoop" on the <cfloop> tag.

A Gotcha: <cfbreak> Inside a Switch

This one is worth knowing explicitly: in tag syntax, if <cfbreak> is used inside a <cfcase> that's nested inside a loop, it breaks the enclosing loop, not just the switch. This is different from CFScript, where a plain break; inside a switch's case only exits the switch, matching the behavior you'd expect from most other languages.

NOTE

If you need to exit a switch case in tag syntax without affecting a surrounding loop, structure the logic so <cfbreak> isn't needed inside the <cfcase> at all — most switch blocks fall through naturally at the end of a case.

Real-World Example: Search and Stop

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

Once the match is found, there's no reason to keep checking the rest of the array — break stops the wasted work.

Common Beginner Mistakes

Expecting break to exit every level of a nested loop

By default it only exits the loop it's directly inside. If you need to exit an outer loop too, label that outer loop and break the label by name.

Assuming <cfbreak> in a switch behaves like CFScript's break

Inside a loop, <cfbreak> used within a <cfcase> breaks the enclosing loop, not just the switch — a real difference from CFScript's break; in a switch case, which only exits the switch.

Using break where continue was actually needed

break stops the loop entirely. If you only want to skip the current item and keep looping, use continue instead.

Best Practices

  • Use break as soon as further looping is pointless — searching, validating, or checking a limit.
  • Give labels clear, descriptive names (outerLoop, rowLoop) rather than single letters.
  • Keep an eye on <cfbreak> placement relative to <cfcase> if that loop-breaking behavior isn't what you want.

Interview Questions

What does break do?

It exits the current loop immediately, skipping any remaining iterations.

Does break exit all nested loops by default?

No — only the loop it's directly inside. Exiting an outer loop requires labeling it and breaking that label by name.

How do you label a loop in CFScript?

Place an identifier followed by a colon directly before the loop, e.g. outerLoop: for (...) { ... }.

What's the tag-syntax equivalent of break?

<cfbreak>, with an optional label attribute to target a specific labeled outer loop.

Summary

In this lesson, you learned how break exits a loop immediately in both CFScript and tag syntax, how it only affects the innermost loop by default, and how to use a label to break a specific outer loop from a nested one. You also saw a real difference in how <cfbreak> behaves inside a switch case compared to CFScript's break.

What's Next?

The next lesson covers continue — skipping the rest of the current iteration without exiting the loop entirely.