User Guide

Table Of Contents
Flow control 37
The following example shows switch processing:
<cfoutput query = "GetEmployees">
<cfswitch expression = #Department#>
<cfcase value = "Sales">
#FirstName# #LastName# is in <b>Sales</b><br><br>
</cfcase>
<cfcase value = "Accounting">
#FirstName# #LastName# is in <b>Accounting</b><br><br>
</cfcase>
<cfcase value = "Administration">
#FirstName# #LastName# is in <b>Administration</b><br><br>
</cfcase>
<cfdefaultcase>#FirstName# #LastName# is not in Sales,
Accounting, or Administration.<br>
</cfdefaultcase>
</cfswitch>
</cfoutput>
cfloop and cfbreak
The
cfloop tag loops through the tag body zero or more times based on a condition specified by
the tag attributes. The
cfbreak tag exits a cfloop tag.
cfloop
The
cfloop tag provides five types of loops:
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:
1.
Sets up a ten-element array with the word “kumquats” in the fourth entry.
2.
Loops through the array until it encounters an array element containing “kumquats” or it
reaches the end of the array.
3.
Prints out the value of the Boolean variable that indicates whether it found the word kumquats
and the array index at which it exited the loop.
Loop type Description
Index Loops through the body of the tag and increments a counter variable by a
specified amount after each loop until the counter reaches a specified value.
Conditional Checks a condition and runs the body of the tag if the condition is True.
Query Loops through the body of the tag once for each row in a query.
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.