Loop type
Description
List
Loops through the body of the tag once for each entry in a list.
Collection
Loops through the body of the tag once for each key in a ColdFusion structure or
item in a COM/DCOM object.
The following example shows a simple index loop:
<cfloop index = "LoopCount" from = 1 to = 5>
The loop index is <cfoutput>#LoopCount#</cfoutput>.<br>
</cfloop>
The following example shows a simple conditional loop. The code does the following:
Sets up a ten-element array with the word "kumquats" in the fourth entry.
1
Loops through the array until it encounters an array element containing "kumquats" or it
2
reaches the end of the array.
Prints out the value of the Boolean variable that indicates whether it found the word kumquats
3
and the array index at which it exited the loop.
<cfset myArray = ArrayNew(1)>
<!--- Use ArraySet to initialize the first ten elements to 123 --->
<cfset ArraySet(myArray, 1, 10, 123)>
<cfset myArray[4] = "kumquats">
<cfset foundit = False>
<cfset i = 0>
<cfloop condition = "(NOT foundit) AND (i LT ArrayLen(myArray))">
<cfset i = i + 1>
<cfif myArray[i] IS "kumquats">
<cfset foundit = True>
</cfif>
</cfloop>
<cfoutput>
i is #i#<br>
foundit is #foundit#<br>
</cfoutput>
Note: You can get an infinite conditional loop if you do not force an end condition. In this example, the
loop is infinite if you omit the <cfset i = i + 1> statement. To end an infinite loop, stop the
ColdFusion application server.
cfbreak
The
tag exits the
cfbreak
particular condition occurs. The following example shows the use of a
loop:
<cfloop query="fruitOrder">
<cfif fruit IS "kumquat">
<cfoutput>You cannot order kumquats!<br></cfoutput>
<cfbreak>
</cfif>
<cfoutput>You have ordered #quantity# #fruit#.<br></cfoutput>
</cfloop>
tag. You typically use it in a
cfloop
tag to exit the loop if a
cfif
tag in a query
cfbreak
Flow control
49
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers