<cfloop> is the one loop tag in ColdFusion — it doesn't have separate tags for a for loop, while loop, or foreach. Instead, which attributes you give it decide what kind of loop it becomes.
This lesson is a complete reference: the index and condition loops were already introduced (as the tag-syntax side of the for and while loops), and this lesson adds list, array, collection, query, file, and times loops, plus the query loop's grouping feature.
Learning Objectives
After completing this lesson, you'll be able to:
- Recognize all the loop types <cfloop> supports and their key attributes.
- Loop over a delimited list with custom delimiters.
- Loop over an array using the array attribute.
- Loop over a struct with the collection attribute.
- Loop over query rows, including grouping by a column.
- Loop through a file line by line.
- Use a times loop to repeat a fixed number of times.
The Loop Types at a Glance
| Loop type | Key attributes | Purpose |
|---|---|---|
| Index | index, from, to, step | Count through a range of numbers |
| Condition | condition | Repeat while an expression is true |
| List | list, index, delimiters | Iterate delimited string values |
| Array | array, index, item | Iterate array elements |
| Collection | collection, item, index | Iterate a struct's keys (and values) |
| Query | query, startrow, endrow, group | Iterate query result rows |
| File | file, index, characters | Read a file line by line |
| Times | times | Repeat a fixed number of times |
Index and Condition Loops (Recap)
These were covered in depth in the for loop and while loop lessons — a quick recap here for completeness.
List Loop
Iterates over a delimited string. The delimiters attribute defaults to a comma, and accepts multiple characters — each one is treated as a separator.
Array Loop
Collection (Struct) Loop
Struct key order isn't guaranteed — don't rely on keys coming back in the order you defined them.
Query Loop
The query attribute loops through every row of a query result. Inside the loop, column names become available directly, with no need to prefix them.
Limiting Rows: startrow, endrow, maxrows
Use startrow and endrow to loop over only part of a result set. maxrows is an alternative to endrow — you can use one or the other, not both together.
<cfloop query="employees" startrow="2" endrow="4">
<cfoutput>#name#<br></cfoutput>
</cfloop>Grouping Query Results
The group attribute collapses adjacent rows that share the same value in that column — useful for displaying grouped reports, provided the query is sorted by that column first (usually with ORDER BY). A nested <cfloop> without a group then iterates the rows within each group.
group only makes sense on a query that's already sorted by that column — grouping doesn't sort the data for you.
File Loop
Reads a file line by line without loading the whole thing into memory at once — useful for processing large log or data files.
<cfloop file="C:\logs\access.log" index="line">
<cfoutput>#line#<br></cfoutput>
</cfloop>Times Loop
A simple fixed-count loop — an alternative to an index loop from 1 to N when you don't need the counter's value inside the loop.
times is a newer addition (Lucee 5+) — check your target ColdFusion engine's version if you need broad compatibility.
Real-World Example: Grouped Product Report
Common Beginner Mistakes
Using endrow and maxrows together on a query loop
These two attributes conflict — use one or the other to limit how many rows the loop processes, not both at once.
Using group on data that isn't sorted by that column
group only collapses adjacent rows with the same value — if the query isn't sorted by that column first, rows with the same value that aren't next to each other will show up as separate groups.
Forgetting the delimiters attribute name is plural
It's delimiters, not delimiter — a common typo that silently falls back to the default comma delimiter instead of erroring.
Best Practices
- Pick the most specific loop type for the data you have — array, collection, or query — rather than converting everything into a list.
- Sort a query with ORDER BY before using group, since grouping doesn't sort for you.
- Use a file loop instead of reading an entire large file into a string first, to avoid loading it all into memory at once.
Interview Questions
How many loop tags does ColdFusion have in tag syntax?
Just one — <cfloop>. Which loop type it performs depends on which attributes you supply.
What does the group attribute do on a query loop?
It collapses adjacent rows that share the same value in that column, provided the query result is already sorted by that column.
Can you use endrow and maxrows together?
No — they're alternative ways to limit the row range, not meant to be combined.
What's the default delimiter for a list loop if you don't specify delimiters?
A comma.
Summary
In this lesson, you saw the complete range of what <cfloop> can do: index and condition loops (recapped from earlier lessons), plus list, array, collection, query (including grouping), file, and times loops. A single tag, shaped by whichever attributes you give it.
What's Next?
The next lesson covers break — how to exit a loop immediately, including how it behaves inside nested loops.