Home arrow ColdFusion arrow Page 4 - What You Must Know About ColdFusion Flow-Controls

What You Must Know About ColdFusion Flow-Controls

Flow control tags exist to control how a program proceeds. Coldfusion uses conditional processing tags to control the flow. This article covers some of the more important flow control tags in the language, as well as script equivalents.

Author Info:
By: Jayaram Krishnaswamy
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
September 14, 2005
  1. · What You Must Know About ColdFusion Flow-Controls
  2. · What if, and where to scenarios
  3. · Which way to go?
  4. · Keep looking until you find
  5. · Breaking out of loops
  6. · Call a halt and end

print this article

What You Must Know About ColdFusion Flow-Controls - Keep looking until you find
(Page 4 of 6 )

Searching through a group of objects until you find the desired object is a commonplace action. In programming this is achieved by resorting to the technique called loop. In this technique you go through the group of objects one at a time, repeatedly displaying what you find until such time as you have exhausted all the items, or found what you wanted. You could also use this technique to count objects. The loop construct is implemented by the <cfloop.attributes.../> construct

The CFML's <cfloop> tag

ColdFusion documents three kinds of loops. The attributes of the <cfloop.../> determine which kind of loop is implemented. This is a list of attributes (parameters) that are available for <cfloop.../>. We will be discussing in this tutorial only the index and conditional loops and the 'looping over collection' will be postponed until queries, structures, and so on are discussed.

  • Index Loop (parameters: index, from, to, step)
  • Conditional Loop (parameter: condition)
  • Looping over a Collection[/Query/structure/list] (parameter: depends on sub-types)
Index Loop

The processing is performed from the beginning value of the index to the end value of the index, incrementing (or advancing) the process by the step (by the increment). The processing ends after reaching the end value of the idnex.

Syntax of an Index Loop
<cfloop index = "parameter name"
from = "beginning value"
to = "ending value"
step = "increment">
Process the items here
Example 9

The range of the index is from 1 to 5 including 1 and 5. It starts with 1 goes into the block where it encounters a text followed by the variable which has the index value(1) at that instant and after this it is automatically incremented by 1(1 is default). This is repeated till it's value is equal to 5.

<cfloop index = "Count" from = "1" to = "5">
<cfoutput>You are now at: #Count#.

The displayed output when this code runs is:

You are now at: 1.
You are now at: 2.
You are now at: 3.
You are now at: 4.
You are now at: 5.

The step value can be chosen as necessary, and so are the values of from and to. It is easy to see that you could count down, as well, using a negative step and a higher from value, and the displayed output when you run the code is the reverse of the previous example.

<cfloop index = "Count" from = "5" to = "1" step=-1>
<cfoutput>You are now at: #Count#.

While looping, you could also embed further conditional statements so that you get a more focused output. Review the following code. You will see that although the looping is effective, the output is written out only when the count exceeds the value 3, that is for index 4 and index 5.

<cfloop index = "Count" from = "1" to = "5" >
<cfif #Count# gt 3>
<cfoutput>You are now at: #Count#.
Conditional Loop

In this case, the loop iteration (that means the act of going through the loop) is carried out until such time as a certain condition is attained. In the previous case of Index loop we saw attributes like from, to, index and step. In this case the attribute is condition. The syntax is as follows;

<cfloop    condition = "expression">
...Process this as long as condition is satisfied..
Example 10

We start off with the variable vol having a initial value of 5. When we enter the loop we notice that the loop will be active as long as this variable (vol) is less than 10. We enter inside the loop, write out "Count is 5" and in the next line the count becomes 6 (incremented in the cfset). Now we go back and check the condition; it is now 6 but still less than 10, so you enter the loop again and so on until vol becomes 10. Since 10 cannot be less than 10, we are stopped. This should remind you of the while() ..wend construct in Visual Basic, or similar constructs in JavaScript.

<cfset vol=5>
<cfloop condition="#vol# lt 10">
<cfoutput>Count is  #vol#</cfoutput><br/>
<cfset #vol#=#vol#+1>

The displayed output when this runs is shown here. Notice that 10 did not print.

Count is 5
Count is 6
Count is 7
Count is 8
Count is 9

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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