Loops while the condition is true or until break.

  <cfwhile [condition=boolean] [label=string]>

 while(condition) { }

This tag requires Lucee.  Not supported on Adobe ColdFusion, etc.

Attribute Reference

condition boolean

A boolean condition that determines if the loop should continue.

label string

Used to address loop from break or continue statement instead of innermost loop.

Sample code using the cfwhile tag

Output the value of x while it is less than 5.

<cfset x = 0/>
<cfwhile condition="#x LT 5#">
  <cfset x = x + 1/>

Expected Result: 0 1 2 3 4

An outer while loop can be broken from inside a nested loop if the correct label name is passed within the break tag.

<cfset row = 0/>
<cfwhile condition="#row LT 5#" label="outerLoop">
    <cfset col = 0/>
    <cfwhile condition="#col LT 5#" label="innerLoop">

        <cfif col EQ 3>
        <cfelseif row EQ 1>
            <cfbreak outerLoop/>

        <cfoutput>[#row#, #col#]</cfoutput>

        <cfset col = col + 1/>
    <cfset row = row + 1/>

Expected Result: [0, 0] [0, 1] [0, 2]

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub