View comments | RSS feed
Contents > CFML Reference > ColdFusion Tags > cfbreak PreviousNext

cfbreak

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

Flow-control tags

<cfbreak>

cfabort, cfexecute, cfif, cflocation, cfloop, cfswitch, cfthrow, cftry

<!--- 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> 

Contents > CFML Reference > ColdFusion Tags > cfbreak PreviousNext

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

Version 6.1

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

Comments


Steve said on Feb 17, 2004 at 8:14 AM :
i just found out today that cfbreak *also* breaks out of a cfswitch statement which it didn't used to do. So if you have a cfswitch within a cfloop and call the cfbreak inside of one of the cfcases, it will keep on looping.

Personally I think this was added to the language with false logic. In most other languages the break; statement is associated with cases. i.e. in c++ it's like this:

switch (expr) {
case c1:
statements // do these if expr == c1
break;
case c2:
statements // do these if expr == c2
break;
}

Where the break; separates the various case statements. but in cfml, we already have a </cfcase>
<cfswitch expression="#expr#">
<cfcase value="c1">
statements // do these if expr == c1
</cfcase>
<cfcase value="c2">
statements // do these if expr == c2
</cfcase>
</cfswitch>

Adding a <cfbreak> inside of the case, is the same thing as closing the cfcase.

This makes CFMX not backwards compatible with CF5.
halL said on Feb 18, 2004 at 1:16 PM :
This is a known issue in ColdFusion MX 6.1 (bug number 53339).
wlee said on Mar 22, 2004 at 10:45 AM :
We are working to improve the examples in the ColdFusion reference pages. We propose to replace the current example on this page with the the following example. If you have any comments on this example, add them to this page.


<!--- 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 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="cfbreak.cfm" method="POST">
<select name="courseNum">
<cfoutput query="GetCourses">
<option value="#Number#">#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.Number IS form.courseNum>
<cfoutput>
<h4>Your Desired Course was found:</h4>
<pre>#Number# #Descript#</pre>
</cfoutput>
<cfbreak>
<cfelse>
<br> Searching...
</cfif>
</cfloop>
</cfif>
Didgiman said on Aug 31, 2004 at 6:35 AM :
Why isn't there a 'cfcontinue' tag?
extdw_doc said on Sep 1, 2004 at 10:23 AM :
cfcontinue is enhancement request 52228.

 

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

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-pa7.htm