DevLearningTools

🚧 This site is under active construction — new tools, guides, and pages are added every week.

MODULE 4 · LESSON 05

<cfloop>

The complete <cfloop> reference — every loop type it supports in tag syntax: index, condition, list, array, collection/struct, query (with grouping), file, and times.

New lessons are added one at a time as the course gets built out — a graded quiz for each lesson is still on the way.

<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 typeKey attributesPurpose
Indexindex, from, to, stepCount through a range of numbers
ConditionconditionRepeat while an expression is true
Listlist, index, delimitersIterate delimited string values
Arrayarray, index, itemIterate array elements
Collectioncollection, item, indexIterate a struct's keys (and values)
Queryquery, startrow, endrow, groupIterate query result rows
Filefile, index, charactersRead a file line by line
TimestimesRepeat 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.

Index loop
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Condition loop
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

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.

Default comma delimiter
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Custom delimiter
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Array Loop

Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Collection (Struct) Loop

Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

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.

Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

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.

Tag Syntax
<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.

Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

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.

Tag Syntax
<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.

Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

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

Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

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.