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> tagColdFusion 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 LoopThe 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
...
</cfloop>
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#.
</cfoutput><br>
</cfloop>
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#.
</cfoutput><br>
</cfloop>
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#.
</cfoutput>
</cfif>
<br>
</cfloop>
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..
</cfloop>
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>
</cfloop>
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
Next: Breaking out of loops >>
More ColdFusion Articles
More By Jayaram Krishnaswamy