Flow control

ColdFusion provides several tags that let you control how a page gets executed. These tags generally correspond to programming language flow control statements, such as if, then, and else. The following tags provide ColdFusion flow control.
Tags
Purpose
cfif, cfelseif, cfelse
Select sections of code based on whether expressions are True or False.
cfswitch, cfcase, cfdefaultcase
Select among sections of code based on the value of an expression. Case processing is not limited to True and False conditions.
cfloop, cfbreak
Loop through code based on any of the following values: entries in a list, keys in a structure or external object, entries in a query column, an index, or the value of a conditional expression.
cfabort, cfexit
End processing of a ColdFusion page or custom tag.

This section provides a basic introduction to using flow-control tags. CFScript also provides a set of flow-control statements. For information on using flow-control statements in CFScript, see Chapter 6, "Extending ColdFusion Pages with CFML Scripting". For more details on using flow-control tags, see the reference pages for these tags in CFML Reference.

cfif, cfelseif, and cfelse

The cfif, cfelseif, and cfelse tags provide if-then-else conditional processing, as follows:

  1. The cfif tag tests a condition and executes its body if the condition is True.
  2. If the preceding cfif (or cfelseif) test condition is False, the cfelseif tag tests another condition and executes its body if that condition is True.
  3. The cfelse tag can optionally follow a cfif tag and zero or more cfelseif tags. Its body executes if all the preceding tags' test conditions are False.

The following example shows the use of the cfif, cfelseif, and cfelse tags. If the value of the type variable is "Date," the date displays; if the value is "Time," the time; displays otherwise, both the time and date display.

<cfif type IS "Date">
  <cfoutput>#DateFormat(Now())#</cfoutput>
<cfelseif type IS "Time">
  <cfoutput>#TimeFormat(Now())#</cfoutput>
<cfelse>
  <cfoutput>#TimeFormat(Now())#, #DateFormat(Now())#</cfoutput>
</cfif>

cfswitch, cfcase, and cfdefaultcase

The cfswitch, cfcase, and cfdefaultcase tags let you to select among different code blocks based on the value of an expression. ColdFusion processes these tags as as follows:

  1. The cfswitch tag evaluates an expression. The cfswitch tag body contains one or more cfcase tags and optionally includes cfdefaultcase tag.
  2. Each cfcase tag in the cfswitch tag body specifies a value or set of values. If a value matches the value determined by the expression in the cfswitch tag, ColdFusion runs the code in the body of the cfcase tag and then exits the cfswitch tag. If two cfcase tags have the same condition, ColdFusion generates an error.
  3. If none of the cfcase tags match the value determined by the cfswitch tag, and the cfswitch tag body includes a cfdefaultcase tag, ColdFusion runs the code in the cfdefaultcase tag body.

Note:   Although the cfdefaultcase tag does not have to follow all cfcase tags, it is good programming practice to put it at the end of the cfswitch statement.

The cfswitch tag provides better performance than a cfif tag with multiple cfelseif tags, and is easier to read. Switch processing is commonly used when different actions are required based on a a string variable such as a month or request identifier.

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:
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 Tru.e
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.

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.
<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 cfbreak tag exits the cfloop tag. You typically use it in a cfif tag to exit the loop if a particular condition occurs. The following example shows the use of a cfbreak tag in a query 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>

cfabort and cfexit

The cfabort tag stops processing of the current page at the location of the cfabort tag. ColdFusion returns to the user or calling tag everything that was processed before the cfabort tag. You can optionally specify an error message to display. You can use the cfabort tag as the body of a cfif tag to stop processing a page when a condition, typically an error, occurs.

The cfexit tag controls the processing of a custom tag, and can only be used in ColdFusion custom tags. For more information see, "Terminating tag execution," in Chapter 10 and CFML Reference.

Comments