DevLearningTools

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

MODULE 4 · LESSON 02

while Loop

Repeating code for as long as a condition stays true in ColdFusion — CFScript's while loop and its tag-syntax equivalent, the <cfloop> condition 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.

A while loop repeats a block of code for as long as a condition stays true. Unlike a for loop, you don't need to know in advance how many times it'll run — it keeps going until the condition becomes false.

This makes it the right tool when you're waiting for something to happen — a counter reaching a limit, a value being found, or a piece of data running out — rather than counting a fixed number of repetitions.

Learning Objectives

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

  • Write a while loop in CFScript.
  • Write the equivalent condition loop using <cfloop> in tag syntax.
  • Understand why a while loop's condition is checked before each pass.
  • Avoid writing an infinite loop.
  • Use break and continue inside a while loop.

Basic while Loop

A while loop checks its condition before every pass. As soon as the condition is false, the loop stops — including on the very first check, if the condition starts out false.

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

Tag syntax calls this a condition loop — the condition attribute takes the place of the while(...) header.

Why the Counter Update Matters

Unlike a for loop, a while loop doesn't have a built-in "update" step — you're responsible for changing something inside the loop body that eventually makes the condition false. Forget that, and the loop never stops.

Infinite loop — do not run
<cfscript>

count = 1;

while (count <= 5) {
    writeOutput(count & " ");
    // count++; missing — count never changes
}

</cfscript>
NOTE

This example is intentionally broken to illustrate the mistake — never actually run code like this.

Looping Until a Value Is Found

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

Real-World Example: Retry Logic

while loops are common whenever you're retrying something until it succeeds or you hit a limit — like checking an inventory count before allowing a purchase.

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

break and continue

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

break and continue work the same way inside a while loop as they do inside a for loop.

Common Beginner Mistakes

Forgetting to update the loop's own condition

If nothing inside the loop body changes the value the condition depends on, the loop runs forever (or until the server times out). Always make sure something in the loop moves it toward a false condition.

Writing the condition so it's never true

while (count > 10) when count starts at 1 never runs at all — while checks the condition before the first pass, unlike a do...while loop.

Using = instead of == in the condition

while (found = true) assigns true to found instead of comparing it, which silently changes your variable's value. Use == to compare.

Best Practices

  • Always make sure the loop body changes something the condition depends on.
  • Keep the condition simple and readable — extract complex logic into a well-named variable first if needed.
  • Use a for loop instead if you already know exactly how many iterations you need.
  • Add a safety counter (like attempts < 3 above) when looping based on external conditions that might never resolve.

Interview Questions

When is the condition of a while loop checked?

Before each pass, including the very first one — if the condition starts false, the loop body never runs.

What's the tag-syntax equivalent of a while loop?

The <cfloop> condition loop, using the condition attribute.

What causes an infinite loop?

The loop's condition never becomes false, usually because nothing inside the loop body updates the value the condition depends on.

How is a while loop different from a for loop?

A for loop is built for a known, fixed number of iterations with a built-in counter and update step. A while loop keeps going for as long as a condition holds true, with no built-in counter — useful when the number of iterations isn't known in advance.

Summary

In this lesson, you learned how to write a while loop in CFScript and its tag-syntax equivalent, the <cfloop> condition loop. You saw why the condition is checked before every pass, how forgetting to update it causes an infinite loop, and how to search through data until a value is found.

What's Next?

The next lesson covers the do...while loop — like a while loop, but it always runs the loop body at least once before checking the condition.