• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 4. Looping > What Is Looping?

What Is Looping?

Looping is the use of any control structure to cause a program to repeatedly execute a block of code. You can use many kinds of loops in ColdFusion. The one you choose to implement depends on how you want to loop and on whether you are looping over a data structure. Loops repeat a specific number of times or until a certain condition is met.


Index loops are used to execute a block of code a specified number of times. The number of times a loop repeats is determined by a range of numeric values. In many other languages, index loops are referred to as for loops.


If you have a condition in which you want to jump out of the Index loop, it's best to use a Conditional loop instead. Although you can use ColdFusion's <cfbreak> tag, which permits jumping out of the loop, when it comes to style you are better off not doing so.

The Index loop's attributes are shown in Table 4.1.

Table 4.1. Index Loop Attributes
indexThe name of the variable to hold the current loop value.
fromThe beginning value of the index.
toThe ending value of the index.
stepAn optional attribute that controls the increment of the index each time through the loop. The default is 1.

To loop from 1 to 10, you would use the following statement:

<cfloop index="i" from="1" to="10">

To create a loop that starts at 10 and steps backward in 2s to 4, you would use the following statement:

<cfloop index="i" from="10" to="4" step="-2">


You use the Conditional loop when you want to loop during the time that a certain condition is true. In many other languages, conditional loops are referred to as while loops.

The conditional loop has only one attribute: condition. In it, you place the expression that will be evaluated by ColdFusion as true or false, and thus determines when the loop terminates. Because the condition is checked at the entrance to the loop, the loop may not be executed at all. When you're considering how the loop will behave, it is a good idea to consider how the loop will be entered the first time, and how the loop will terminate.

→ For a complete discussion of writing expressions that will be evaluated as true or false by ColdFusion, see Chapter 3, “Conditional Processing.”

A sample conditional loop follows:

<!--- Set up variables for condition --->
<cfset NumToGet=4>
<cfset i=0>
<!--- Now loop --->
<cfloop condition="i LTE NumToGet">
 <cfset i=i+1>


The loop does not exit at the instant the condition becomes FALSE. It finishes all the statements between the <cfloop> and </cfloop> before terminating.


In the conditional loop shown here, the output is from 1 to 5, not from 0 to 4 as you might expect. The output goes to 5 because of the order in which the increment is done. If the two lines inside the loop were reversed, the output would be from 0 to 4. Be sure to consider this result when using conditional loops.


The query loop loops over a block of statements once for every record read from a query.


The function of the query loop is similar to that of <cfoutput> with a query attribute.

The query loop has the attributes shown in Table 4.2.

Table 4.2. Query Loop Attributes
queryThe name of the query to loop over
startrowAn optional attribute that determines the first row of the query that will be included in the loop
endrowAn optional attribute that determines the last row of the query that will be included in the loop

A sample query loop follows:

<cfloop query="MyData">


As is the case within a <cfoutput> block, the use of the query name as a prefix is optional. However, <cfloop> does not automatically act as <cfoutput>, so those tags are required, as shown in the example.


Lists in ColdFusion are simply sets of data separated by one or more delimiters. You can use a list loop to step over the elements of the list one at a time.

→ For further information on lists, see Chapter 13, “Lists.”

When using a list loop, use the attributes shown in Table 4.3.

Table 4.3. List Loop Attributes
indexThe name of the variable to hold the current element of the list
listThe list to be stepped through
delimitersAn optional attribute that specifies the delimiter or delimiters used to separate items in the list

An example of a list loop follows:

<!--- Create a list --->
<cfset Months="Jan,Feb,Mar,Apr">
<!--- Loop over the list --->
<cfloop index="i" list="#Months#">

The index of the loop, here the variable i, will contain the values of the list Months. Specifically, Jan, Feb, Mar, Apr, and those months' abbreviations will be displayed.

List looping can also be used to loop through words in a sentence, paragraphs in a story, and more by specifying the appropriate delimiters.


Collection loops allow looping over key-value pairs in structures. Each time through the loop, the variable specified in the item attribute takes the value of successive key names. To display the value of the key-value pairs, you use the item with associative array notation. The following example helps clarify the situation:

<!--- Create and fill the structure --->
<cfset product=StructNew()>
<cfset product.Name="Ball">
<cfset product.Color="Red">
<!--- Display the key-value pairs --->
<cfloop collection="#product#" item="TheKey">
 <cfoutput>#TheKey#: #product[TheKey]#</cfoutput><br>

The preceding code creates and populates a structure. In the loop, the variable TheKey takes the values Name and Color, which are the keys in the structure. Associative array notation, #product[TheKey]#, is used to display the values in the structure, Ball and Red.

→ You can find a complete discussion of structures in Chapter 15, “Structures.”


In index and list loops, the attribute that holds the current loop value is index. With collection loops, it is called item.


The collection loop is built to loop over key-value pairs of a structure. When you're looping over arrays, use an Index loop with the TO attribute set equal to #ArrayLen(My_Array)#.

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint