Home arrow ColdFusion arrow Introduction to ColdFusion Markup Language, concluded
COLDFUSION

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).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 41
January 26, 2006
TABLE OF CONTENTS:
  1. · Introduction to ColdFusion Markup Language, concluded
  2. · Query Loop
  3. · Collection Loop
  4. · The cfdirectory tag
  5. · The cffile tag
  6. · The cfform tag

print this article
SEARCH DEVARTICLES

Introduction to ColdFusion Markup Language, concluded
(Page 1 of 6 )

<cfloop>

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

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:

<cfloop index="counter" from="1" to="12">
 
...
</cfloop>

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):

<cfloop index="counter" from="12" to="1" step="-2"> 
  <cfoutput>#counter#<br /></cfoutput> </cfloop>

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

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.


blog comments powered by Disqus
COLDFUSION ARTICLES

- Adobe ColdFusion Just Got More RAD
- How to Access a SQL Anywhere Database with C...
- CFXML: Probing XMLDOM in ColdFusion
- Creating a Web Service with ColdFusion: the ...
- CFAjax: What it is and How to Use it
- Querying SQL 2000 Server from ColdFusion
- Introduction to ColdFusion Markup Language, ...
- Introduction to ColdFusion Markup Language
- Databases and Dreamweaver MX 2004, concluded
- Databases and Dreamweaver MX 2004
- Welcome to Coldfusion MX 6.1, concluded
- Welcome to Coldfusion MX 6.1
- What You Must Know About ColdFusion Flow-Con...
- What You Must Know About Operators in ColdFu...
- Everything You Must Know About ColdFusion Va...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials