What You Must Know About ColdFusion Flow-Controls - Breaking out of loops
(Page 5 of 6 )
The <cfbreak> tag
Sometimes you may not want to cycle through the whole loop, but only verify a few; then you want break out of the loop. While testing your code in loops, often times you may run into a condition of an infinite loop, a condition you may not be able to get out unless you abandon the program by ending it. In such cases, it will be prudent to put in a break which gives you an opportunity to check your code. The switch construct we saw earlier is another place where a break is used. The break is implemented by the <cfbreak> tag. The syntax is:
<cfbreak>
Example 11
This is the same as previous example with a slight modification. After the variable vol gets incremented, it is quizzed in the <cfif> conditional for a value of 7. When the value becomes 7, the if condition is satisfied and the processing reaches <cfbreak>. This is when the loop is broken. Whatever value the variable had up to that time is what is displayed.
<cfset vol=5>
<cfloop condition="#vol# lt 10">
<cfoutput>Count is #vol#</cfoutput><br/>
<cfset #vol#=#vol#+1>
<cfif #vol# gt 7>
<cfbreak>
</cfif>
</cfloop>
The displayed output when this code is run is as follows:
Count is 5
Count is 6
Count is 7
Next: Call a halt and end >>
More ColdFusion Articles
More By Jayaram Krishnaswamy