Contact Us or call 1-877-932-8228


As the name implies, loops are used to loop (or iterate) over code blocks. The following section shows the syntax for some of the different types of loops. All of these loops can be found in FlowControl/Demos/Loops.cfm.

Index Loops

An index loop is like a for loop in other languages. It steps through a process based on some type of count.

<cfloop index="count" from="1" to="5" step="2"> <cfoutput>#count#</cfoutput> </cfloop>

This loop will output 1 3 5.

Conditional Loops

A conditional loop is like a while loop in other languages. It continues to iterate through a process as long as the specified condition is true.

<cfset count=1> <cfloop condition="count LTE 5"> <cfoutput>#count#</cfoutput> <cfset count = count + 2> </cfloop>

This loop will output 1 3 5.

List Loops

Lists are simply strings delimited by a specified character or set of characters. By default, the delimiter is a comma. ColdFusion has many functions for working with lists.

<cfloop> can be used to iterate through a list, performing some function with each list item.

<cfset numlist="1,2,3,4,5"> <cfloop index="num" list="#numlist#"> <cfoutput>#num#</cfoutput> </cfloop> <cfset beatles="paul john ringo george"> <ul> <cfloop index="beatle" list="#beatles#" delimiters=" "> <li><cfoutput>#beatle#</cfoutput></li> </cfloop> </ul>

The first loop will output 1 2 3 4 5. The second loop will output an unordered list of the Beatles' names.

Other Types of Loops

<cfloop> can also be used to loop through other types of data, including queries and structures. Some of these advanced uses of <cfloop> will be covered later in the course.


The <cfbreak> tag is used to break out of loops before they have finished processing. The syntax is shown below:

<cfloop index="count" from="1" to="5" step="1"> <cfoutput>#count#</cfoutput> <cfif count GTE 3> <cfbreak> </cfif> </cfloop>