Introduction to ColdFusion Markup Language, concluded
If you want to get a good start on learning ColdFusion, look no further. This article covers a whole bunch of CFML tags and some basic functions. It is the second of two parts, and excerpted from chapter three of the book ColdFusion Web Development with Dreamweaver MX 2004, written by Jen and Peter deHaan et al. (Apress; ISBN: 1590592379).
Introduction to ColdFusion Markup Language, concluded (Page 1 of 6 )
Loops are typically used to iterate through a Recordset one record at a time, or when you need to execute a block of code a certain number of times. There are five different kinds of<cfloop>s: index loops, conditional loops, query loops, list loops, and collection loops. We cover all five in this section.
Index loops (also known asfor ... nextloops in other languages) are used to loop from a starting value to a predefined end value. The following snippet loops from 1 to 12:
For each iteration of the loop, a variable (in this case#counter#) is set to the value between 1 and 12. For example, by using this kind of loop you can loop from 1 to the number of days in a specific month and build a simple calendar, as you will see in the next chapter. This loop is useful when you know exactly how many times you need to loop through a block of code.
You can also specify an optional step attribute that controls how much the counter is incremented or decremented for each iteration of the loop, as you can see in the following code. By default, the step is 1, which means that the loop increments by 1 each time. For the next iteration, the#counter#variable equals 2, then 3, and so on. If we set a step of -1 or -2, then we will loop backwards by 1 or 2 on each iteration. To test this, type the following code into a new ColdFusion file (indexloop.cfm):
If you run this code in a web browser, you'll get the output shown in Figure 3-10.
Figure 3-10. The numbers output by the index loop
Conditional loops (also known asdo ... whileloops) loop just as long as a condition is true. An example of a conditional loop is as follows (condloop.cfm):
<cfset i = 18> <cfloop condition="i GTE 10"> <cfoutput>#i#<br /></cfoutput> <cfset i = i - 1> </cfloop>
Here we are setting an initial value of#i#to 18 and creating a conditional loop that will loop as long as#i#is greater than or equal to (GTE) 10. For each iteration of the loop, we are displaying the current value of#i#and then decrementing the value of#i#.This loop will display the numbers 18 to 10 to the screen in descending order.
CAUTION Conditional loops can be dangerous! If the end condition cannot be met, the loop does not stop, thus causing what is known as an infinite loop. An infinite loop can cause system instability and eat up a lot of system resources.
Because conditional loops will loop as long as the condition evaluates to true, you can use this behavior to create a controlled infinite loop. This is useful if you don't know exactly how many times you need to loop, or need to exit the loop based on a complex condition. Let's look at an example (condloop2.cfm):
<cfloop condition="TRUE"> <cfset i = RandRange( 1, 10 )> <cfoutput>#i#<br /></cfoutput> <cfif i EQ 10> <cfbreak> </cfif> </cfloop>
By setting the loop condition toTRUE, you are essentially creating an infinite loop. This means that you must manually exit the loop because the end condition is always true. You can exit the loop by using the<cfbreak>tag.
In this example, you are also using a function that you haven't seen yet:#RandRange()#. This function generates a random integer between two specified values. So, youre setting#i#to a random integer between 1 and 10 and outputting the value of#i#to the browser. If#i#is equal to 10, you exit the loop.
You will find out more about#RandRange()#in Chapter 4.