Latest Articles:
Committee Members:
Alert Email
Get a short email alert whenever a new entry is published.
Confidential, secure it's piece of cake to keep uptodate.
Add a little CFCONTINUE to your looping
Loops are the building blocks of what makes a language. They allow you to quickly iterate over data, performing various operations dependent on state. CFML has always had strong support for this construct with CFLOOP, allowing you to pretty much iterate over nearly any type of data structure you care to throw at it.
CFML has also supported the notion of breaking out of a loop. Say you don't want to carry on going around your loop, you can simply CFBREAK out of it and execution is resumed from the end of the CFLOOP tag.
Sometimes that isn't enough and this is where CFML lets the developer down, but OpenBD picks them back up.
What if you want to simply stop the current iteration and move onto the next element in the loop. Most languages have this ability, the notion to say, "continue;" and move onto the next one.
The alternative of course is to write your loop in such a way that you have lots of CFIF statements, all nesting and quickly before you know it, you can get yourself into quite a bit of a nested logic hell.
<cfloop index="x" from="1" to="10">
<cfif x eq 1>
First Element
<cfcontinue>
</cfif>
Now we can do something with elements 2-10
</cfloop>
BlueDragon introduced the CFCONTINUE tag many years ago, which naturally carried on through to OpenBD and is part of the core distribution ready for use. CFCONTINUE operates exactly like it does in any other language that fully supports looping. You can insert it inside your loop anywhere, and it will stop the execution of the current iteration and move onto the next element in the loop.
This proves to be a much more efficient and cleaner way of managing your data looping and has the added benefit of making your code easier to write and read.
Try it today - clean up your loops.
Comments (4)
Glad to hear this is getting added to the spec.
For coldfusion 8 you can use continue in cfscript too. Also it would be helpful to be able to label loops, so that if you have a loop of a loop of a loop then inside the 3rd (deepest) loop you could continue the 2nd loop or the top loop.Do not let the real simplistic example fool you -- yes there is easier ways of writing that, but "continue" is a well known construct that lets you easily write rather complex loops without tying yourself into knots with lots of IF's and bizarre logic.
I have always liked the continue concept, but generally I can never find a use for it.
I'd rather structure the above as... x = 1 // first element for x=2 ; x <= 10 ; x++ // 2 through 10 elements This way there are no conditional statements and it is easier to parse visually.



Bradley, here's a better example of when continue is helpful: skipping comments when looping through an ini-style string...
<cfloop index="CurItem" list="#Data#" delimiters="#Chr(10)#">
</cfloop><cfif refind('^##;',CurItem)><cfcontinue/></cfif>
...