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.