Living Without “continue” in ColdFusion

A decided to try to implement a custom ColdFusion loop tag that would support a custom CFCONTINUE tag. To continue within a loop is similar to breaking (which you can do with CFBREAK) except rather than breaking out of the loop, execution starts again from the top of the loop on the next iteration. Unfortunately, there is no CFCONTINUE tag, so this is something you cannot do in ColdFusion. The following illustrates the use of “continue” in Java:

for (int i = 0; i < 10; ++i) {if (i == 5) {continue;}System.out.println("This number will never be 5: " + i);}

Anyway, it hasn’t occurred to me how to implement continue functionality in 100% ColdFusion (without using a JSP custom tag), however there is usually an easy way to get around CF’s lack of “continue”. Here is the loop above rewritten without continuing:

for (int i = 0; i < 10; ++i) {if (i != 5) {System.out.println("This number will never be 5: " + i);}}

And, of course, the ColdFusion version:

<cfloop from="1" to="10" index="i"><cfif i ne 5>This number is not 5: #i#<br></cfif></cfloop>

Here’s another interesting approach:

<cfset i = 1/><cfloop condition="#i# le 10"><cfif i eq 5><cfset i = i + 1/></cfif>This number is not 5: #i#<br><cfset i = i + 1/></cfloop>

A CFCONTINUE would still be a nice tag to have, but I have yet to run into a situation where I couldn’t pretty easily work around its absence.

6 Responses to Living Without “continue” in ColdFusion

  1. .erki says:

    Here’s 100% CFML solution:<cfloop from=”1″ to=”10″ index=”i”><cftry><cfif i EQ 5><cfthrow type=”continue”></cfif>This number will never be 5: #i#<br><cfcatch type=”continue”><!— ignore —></cfcatch></cftry></cfloop>

  2. barneyb says:

    Part of the benefit of the missing CFCONTINUE tag would be readability. While all the solutions you provided (and erki’s as well) work just dandy, and can probably be applied to pretty much every potential use for the CFCONTINUE tag, the decreased readibility is less than desirable, especially in complex loops. For instance, if you have to potential continue points in a loop. that’ll be one nasty set of CFIF tags.

  3. Tarot Online says:

    It’s Fantastic !!

  4. Damn, I couldn’t get this code working on my server. I’m not sure if it’s an environment thing or if your code itself has something screwed up.I’m going to try it on a windows setup and see if it works there.

  5. Yes I know I’m posting in a 2 year old archive :-PJust wanted to say how #$%$%# up it is that even a CFMX 7 _still_ doesn’t provide a <cfcontinue> hard can it be.This was just to cool down :-)L8rs,