Adobe ColdFusion 8

cfbreak

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>