View comments | RSS feed
Contents > CFML Reference > ColdFusion Tags > cfloop: index loop PreviousNext

cfloop: index loop

An index loop repeats for a number of times that is determined by a numeric value. An index loop is also known as a FOR loop.

<cfloop 
index = "parameter_name"
from = "beginning_value"
to = "ending_value"
step = "increment">
... HTML or CFML code ...
</cfloop>

cfabort, cfbreak, cfdirectory, cfexecute, cfexit, cfif, cflocation, cfrethrow, cfswitch, cfthrow, cftry

Attribute

Req/Opt

Default

Description

index

Required

 

Index value. ColdFusion sets it to from value and increments or decrements by step value, until it equals to value.

from

Required

 

Beginning value of index.

to

Required

 

Ending value of index.

step

Optional

1

Step by which to increment or decrement the index value.

Using anything other than integer values in the from and to attributes of an index loop can product unexpected results. For example, if you increment through an index loop from 1 to 2, with a step of 0.1, ColdFusion outputs "1,1.1,1.2,...,1.9", but not "2". This is a programming language problem regarding the internal representation of floating point numbers.

Note: The to value is evaluated once, when the cfloop tag is encountered. Any change to this value within the loop block, or within the expression that evaluates to this value, does not affect the number of times the loop is executed.

In this example, the code loops five times, displaying the index value each time:

<cfloop index = "LoopCount" from = "1" to = "5">
   The loop index is <cfoutput>#LoopCount#</cfoutput>.<br>
</cfloop>

The output of this loop is as follows:

The loop index is 1. 
The loop index is 2. 
The loop index is 3. 
The loop index is 4. 
The loop index is 5. 

In this example, the code loops four times, displaying the index value each time. The value of j is decreased by one for each iteration. This does not affect the value of to, because it is a copy of j that is made before entering the loop.

<cfset j = 4>
<cfloop index = "LoopCount" from = "1" to = #j#>
   <cfoutput>The loop index is #LoopCount#</cfoutput>.<br>
   <cfset j = j - 1>
</cfloop>

The output of this loop is as follows:

The loop index is 1. 
The loop index is 2. 
The loop index is 3. 
The loop index is 4. 

As before, the value of j is decremented by one for each iteration, but this does not affect the value of to, because its value is a copy of j that is made before the loop is entered.

In this example, step has the default value, 1. The code decrements the index:

<cfloop index = "LoopCount" 
   from = "5" 
   to = "1" 
   step = "-1">
The loop index is <cfoutput>#LoopCount#</cfoutput>.<br>
</cfloop>

The output of this loop is as follows:

The loop index is 5. 
The loop index is 4. 
The loop index is 3. 
The loop index is 2. 
The loop index is 1. 

Contents > CFML Reference > ColdFusion Tags > cfloop: index loop PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


No screen name said on May 14, 2004 at 1:50 PM :
FACT:
cfloop also has more weirdness than is mentioned here.
not only can you not affect the "to" condition during the loop, but you also CANNOT AFFECT THE INDEX.
example:

<cfloop index=i from="1" to="6">
<cfoutput>index is #i#<br></cfoutput>
<cfif i is 5>
<cfset i = 4>
</cfif>
</cfloop>
This should produce an infinite loop, with i going from 5->4->5, but, instead it outputs:
index is 1
index is 2
index is 3
index is 4
index is 5
index is 6

Any changes to the loop index during the execution of the loop are destroyed, and the loop variable is re-created every time based on some secret internal counter.

OPINION:
If only macromedia had knuckles and I were a nun...

bad coldfusion! bad! secret, hidden, undocumented variables, that dont appear in the variables scope, that control the flow of my program.
M@Rob said on Sep 1, 2004 at 1:08 PM :
Hmmm...you sure are fiesty for someone with no screen name. It seems to me that if you set an increment value, you would want it to be predictably incremented. Can you give an practical example of why you would want to do what your example does in real life?
m.ekim said on Nov 7, 2004 at 5:18 PM :
actually, i just came across an instance where i'd like to do this.

<cfloop index="i" from="1" to="#ArrayLen(testArray)#">
<cfif testArray[i].id EQ #id#>
<cfset ArrayDeleteAt(testArray, i)>
</cfif>
</cfloop>
m.ekim said on Nov 7, 2004 at 5:20 PM :
a little more clarification, in this example each element in the array is a structure. i need to delete every element that has an id of #id#
I.Am.Zorglub said on Oct 27, 2006 at 8:53 AM :
@m.ekin: to do something like this loop *backwards* over the array:

<cfloop index="i" from="#ArrayLen(testArray)#" to="1" step="-1">
<cfif testArray[i].id EQ #id#>
<cfset ArrayDeleteAt(testArray, i)>
</cfif>
</cfloop>

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-p77.htm