Adobe ColdFusion 8

cfbreak

View comments | RSS feed

Description

Used within a cfloop tag. Breaks out of a loop.

Category

Flow-control tags

Syntax

<cfbreak>

See also

cfabort, cfexecute, cfif, cflocation, cfloop, cfthrow, cftry; "cfloop and cfbreak" in the ColdFusion Developer's Guide

Example

<!--- This shows the use of cfbreak to exit a loop when a condition is met.--->
<!--- Select courses; use cfloop to find a condition; then break the loop. --->
<!--- Check that number is numeric. --->
<cfif IsDefined("form.course_number")>
    <cfif Not IsNumeric(form.course_number)>
        <cfabort> 
    </cfif> 
</cfif> 
<cfquery name="GetCourses" datasource="cfdocexamples">
    SELECT * 
    FROM Courses 
    ORDER by course_number 
</cfquery>

<p> This example uses CFLOOP to cycle through a query to find a value.
(In our example, a list of values corresponding to courses in the Snippets
datasource). When the conditions of the query are met, CFBREAK stops the loop. </p>
<p> Please enter a Course Number, and hit the "submit" button: </p>
<form action="cfbreak.cfm" method="POST"> 
    <select name="courseNum"> 
        <cfoutput query="GetCourses"> 
            <option value="#course_number#">#course_number#
        </cfoutput> 
    </select> 
    <input type="Submit" name="" value="Search on my Number"> 
</form> 
<!--- If the courseNum variable is not defined, don't loop through the query.--->
<cfif IsDefined ("form.courseNum") IS "True">
<!--- Loop through query until value found, then use CFBREAK to exit query.--->
    <cfloop query="GetCourses"> 
        <cfif GetCourses.course_number IS form.courseNum> 
            <cfoutput> 
                <h4>Your Desired Course was found:</h4> 
                <pre>#course_number# #descript#</pre>
            </cfoutput> 
            <cfbreak> 
        <cfelse> 
            <br> Searching... 
        </cfif> 
    </cfloop> 
</cfif> 



Comments


toomellow said on Feb 15, 2008 at 1:43 PM :
cfbreak in CF8 can cause problems when used inside a cfcase tag (in a cfswitch statement). Although using cfbreak to break out of a case statement worked in previous versions of ColdFusion, it can cause unexpected results including loss of variable references. For CF8 it is advised to remove <cfbreak> tags from within your <cfcase> tags as they are unneccessary (there is apparently no fall-through with ColdFusion case statements as in some other programming languages).

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/8/htmldocs/Tags_a-b_9.html