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:



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.

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

cfif, cfelseif, and cfelse

The cfifcfelseif, 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 cfelsetags. 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.

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

cfswitch, cfcase, and cfdefaultcase

The cfswitchcfcase, and cfdefaultcase tags let you select among different code blocks based on the value of an expression. ColdFusion processes these tags 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 place 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 string variable such as a month or request identifier.
The following example shows switch processing:

<cfswitch expression = #Department#>
<cfcase value = "Sales">
#FirstName# #LastName# is in <b>Sales</b><br><br>
<cfcase value = "Accounting">
#FirstName# #LastName# is in <b>Accounting</b><br><br>
<cfcase value = "Administration">
#FirstName# #LastName# is in <b>Administration</b><br><br>
<cfdefaultcase>#FirstName# #LastName# is not in Sales,
Accounting, or Administration.<br>

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.


The cfloop tag provides the following types of loops:

Loop type



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.


Checks a condition and runs the body of the tag if the condition is True.


Loops through the body of the tag once for each row in a query.

List, file, or array

Loops through the body of the tag once for each entry in a list, each line in a file, or each item in an array.


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:

The loop index is <cfoutput>#LoopCount#</cfoutput>.<br>

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 the value of the Boolean variable that indicates whether it found the word kumquatsand the array index at which it exited the loop.

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.


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:

<cfif fruit IS "kumquat">
<cfoutput>You cannot order kumquats!<br></cfoutput>
<cfoutput>You have ordered #quantity# #fruit#.<br></cfoutput>

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 Executing custom tags and the CFML Reference.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy