Skip to main content
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with: Andy Matthews and Jason Dean and Todd Rafferty
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with: Andy Matthews ( @commadelimited ) Jason Dean ( @JasonPDean ) Todd Rafferty ( @webRat )

CFLoop Attributes Evaluated Only Once

By on
Tags:

For index loops in ColdFusion that are looping over an array or list, I always put the ArrayLen() or ListLen() in the TO attribute. I always felt a little uneasy about it because I thought maybe the function was getting evaluated for each iteration of the index loop. My fear was that this was adding additional processing overhead for each iteration.

Well, I finally got my out of shape butt off the proverbial couch and actually tested what was going on:

<cfoutput>

	<!--- Create an initial array of items. --->
	<cfset arrItems = ListToArray( "ben,dave,mark,mike" ) />

	Original Length: #ArrayLen( arrItems )#<br />
	<br />

	<!---
		Loop over the items in the array. For each iteration, add
		an item to the array to see if the loop will keep going.
		In the TO attribute, use the Min() method to ensure that
		the loop doesn't go on infinitely.
	--->
	<cfloop
		index="intIndex"
		from="1"
		to="#Min( 10, ArrayLen( arrItems) )#"
		step="1">

		<!--- Output the iteration values. --->
		Iteration #intIndex#. Array Length: #ArrayLen( arrItems )#<br />

		<!--- Add an Item to the array to see if we keep looping. --->
		<cfset ArrayAppend(
			arrItems,
			"sarah"
			) />

	</cfloop>

</cfoutput>

Turns out, the loop only iterates 4 times; the attributes only get evaluated that first time. When you stop to think about it, that makes a lot of sense; it is just a string. And if you take Conditional loops into account, it makes sense that the condition="" attribute is a string, not an evaluation. That way it can do a sort of delayed evaluation on the condition.

Man, understanding the basics is so important.

Want to use code from this post? Check out the license.

Reader Comments

3 Comments

Hi Ben,

An "oldie but a goodie"... funny enough we just had a discussion about this at work, what with JavaScript evaluating at each iteration of a for loop it stems to reason that CF would do something similar. The Docs here are of course the best reference.

A conditional loop is defined with a condition = "expression" with an expression one expects evaluation per iteration.

In an index loop the docs clearly state for from,to "starting value", and "ending value", to me this clearly means a fixed value or a variable/expression that is evaluated once.

Your test just confirms it.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel