This module covered five ways to repeat code in ColdFusion: for, while, do...while, for...in, and the full range of <cfloop> tag variants. They can mostly all be made to solve the same problem โ the real question is which one makes the code easiest to read and hardest to get wrong.
The deciding factor almost always comes down to one thing: what do you actually know about the loop before it starts? Do you know the exact number of iterations? Are you waiting on a condition? Do you need the body to run at least once regardless? The answer points you straight at the right loop.
Learning Objectives
After completing this lesson, you'll be able to:
- Match a real situation to the loop type that fits it best.
- Explain why picking the wrong loop type makes code harder to read, even when it technically works.
- Choose the right <cfloop> variant for a given data structure.
The Decision
Decision Table
| Situation | Best choice | Why |
|---|---|---|
| You know exactly how many times to repeat | for | Counter, condition, and update all live together in one line |
| You're repeating until a condition becomes false, and don't know the count ahead of time | while | Checks the condition before every pass, including the first |
| The loop body must run at least once, no matter what | do...while | The only loop type that checks its condition after running |
| You just need each item from an array, struct, list, or query row | for...in (enhanced loop) | No index to declare, check, or update |
| Writing tag-based CFML, not CFScript | <cfloop> (matching variant) | The only loop mechanism tag syntax has |
| Displaying query results grouped by a column | <cfloop query="..." group="..."> | Built-in adjacent-row grouping, no manual bookkeeping |
| Reading a large file without loading it all into memory | <cfloop file="..."> | Streams the file line by line |
| Repeating a fixed number of times, without needing the counter's value | <cfloop times="n"> | Shorter than an index loop when you'll never reference the index |
Why It's Worth Picking Deliberately
Almost any of these loops can be forced to do the same job โ you could write a while loop that manually tracks an index and behaves exactly like a for loop. But that makes the reader do extra work: they have to trace through the logic to figure out "oh, this is really just counting from 1 to 10" instead of seeing it immediately from a for loop's header.
Picking the loop type that matches what you actually know up front makes the code communicate its own intent โ a for loop announces "this repeats N times," a while loop announces "this repeats until something changes," and a for...in loop announces "I don't care about position, just give me each item."
Worked Examples
Three versions of a similar task, each suited to a different loop type.
<cfscript>
// Generate 5 random invite codes
for (i = 1; i <= 5; i++) {
writeOutput(createUUID() & "<br>");
}
</cfscript><cfscript>
// Keep rolling a die until a 6 turns up
attempts = 0;
roll = 0;
while (roll != 6) {
roll = randRange(1, 6);
attempts++;
}
writeOutput("Rolled a 6 after " & attempts & " tries");
</cfscript><cfscript>
products = ["Keyboard", "Mouse", "Monitor"];
for (product in products) {
writeOutput(product & "<br>");
}
</cfscript>Common Beginner Mistakes
Using a while loop with a manual counter instead of a for loop
i = 1; while (i <= 10) { ...; i++; } works, but it splits the counter, condition, and update across three separate places instead of one line โ a for loop says the same thing more clearly.
Using a manual for loop just to get each array item
for (i = 1; i <= arrayLen(arr); i++) { item = arr[i]; ... } works, but for...in hands you the item directly without the extra index variable, when you don't actually need the position.
Reaching for do...while out of habit
If it would ever be wrong for the loop body to run when the condition starts false, that's a sign you actually need a plain while loop, not do...while.
Best Practices
- Ask "what do I actually know before this loop starts?" first โ the answer usually picks the loop type for you.
- Default to for...in for arrays, structs, lists, and query rows unless you specifically need the index.
- Reach for do...while only when skipping the first run would genuinely be wrong.
- In tag syntax, match the <cfloop> variant to the data type you have (array, collection, query) rather than converting everything to a list.
Interview Questions
How do you decide between a for loop and a while loop?
If you know the exact number of iterations ahead of time, use a for loop. If you're repeating until a condition changes and don't know the count in advance, use a while loop.
When would you choose do...while over while?
Only when the loop body must run at least once regardless of whether the condition is true โ for anything else, a while loop is the safer default since it won't run at all if the condition starts false.
Why prefer for...in over a manual for loop for an array?
It removes the need to declare, check, and update an index variable by hand, and it makes the code's intent โ "process each item" โ clearer to read.
Summary
There's rarely a single "correct" loop for a given problem โ several will work. The one worth picking is whichever one matches what you actually know before the loop starts, since that's what makes the code communicate its own purpose instead of making the reader work it out.
This closes out the Loops module.
What's Next?
The next module covers Collections โ starting with Arrays, ColdFusion's ordered lists of values.