View comments | RSS feed

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, cfswitch, cfthrow, cftry

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.number")>
  <cfif Not IsNumeric(form.number)>
    <cfabort> 
    </cfif> 
  </cfif> 
<cfquery name="GetCourses" datasource="cfsnippets">
  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> Please enter a Course Number, and hit the "submit" button: 
<form action="index.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> 

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


No screen name said on Jan 31, 2005 at 2:08 PM :
What happens with <cfbreak> when you are within multiple nested loops? Does is just break the innermost loop? Or does a single <cfbreak> affect all of the loops? This is not clear.

 

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

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/Tags-pt18.htm