DevLearningTools

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

MODULE 4 · LESSON 01

for Loop

The classic counting loop in ColdFusion — CFScript's C-style for loop and its tag-syntax equivalent, the <cfloop> index loop, with step, break, and continue.

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.

A for loop repeats a block of code a specific number of times — the most common loop when you already know (or can calculate) how many times you need to repeat something, like looping through the positions of an array or counting from 1 to 10.

ColdFusion supports the classic C-style for loop in CFScript, and an equivalent index loop in tag syntax using <cfloop>.

Learning Objectives

After completing this lesson, you'll be able to:

  • Write a for loop in CFScript.
  • Write the equivalent index loop using <cfloop> in tag syntax.
  • Use the step attribute to count by more than 1, or count backward.
  • Loop through an array using a for loop.
  • Use break and continue to control loop execution.

Basic for Loop

CFScript's for loop has three parts, separated by semicolons: a starting point, a condition that must stay true, and what happens after each pass.

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

Tag syntax calls this an index loop — from, to, and index replace the three-part CFScript header.

The step Attribute

Use step to count by more than 1, or count backward with a negative step.

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

Counting Backward

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

For a descending loop, step must be negative — ColdFusion won't count down on its own.

Looping Through an Array

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

ColdFusion arrays are 1-indexed, not 0-indexed — the first element is fruits[1], not fruits[0].

break and continue

break exits the loop immediately. continue skips the rest of the current pass and moves to the next one. In tag syntax, these are the <cfbreak> and <cfcontinue> tags.

CFScript — break
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
CFScript — continue
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Real-World Example: Multiplication Table

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

Common Beginner Mistakes

Forgetting to update the counter

for (i = 1; i <= 5;) { writeOutput(i); } never increments i, so it loops forever. Always include i++ (or a step) as the third part.

Off-by-one errors

for (i = 1; i < 5; i++) only runs for i = 1 to 4, skipping 5, because < excludes the final value. Use <= if you want the loop to include the endpoint.

Assuming arrays start at index 0

fruits[0] doesn't exist in CFML — arrays start at fruits[1]. A loop written for (i = 0; i < arrayLen(fruits); i++) will error on fruits[0] and also miss the last element.

Best Practices

  • Use <= when you want the loop to include the endpoint, < when you don't.
  • Prefer meaningful loop variable names (row, i, idx) over single letters in longer loops.
  • Use step for clarity instead of writing i = i + 2 inside the loop body.
  • Use break to exit early once you've found what you're looking for, instead of looping needlessly.

Interview Questions

What are the three parts of a CFScript for loop?

An initializer (e.g. i = 1), a condition (e.g. i <= 5), and an update expression (e.g. i++), each separated by a semicolon.

What's the tag-syntax equivalent of a for loop?

The <cfloop> index loop, using the index, from, to, and optionally step attributes.

What does break do inside a loop?

It exits the loop immediately, skipping any remaining iterations.

What does continue do inside a loop?

It skips the rest of the current iteration and moves on to the next one, without exiting the loop.

What index does a ColdFusion array start at?

1, not 0 — ColdFusion arrays are 1-indexed.

Summary

In this lesson, you learned how to write a for loop in CFScript and its tag-syntax equivalent, the <cfloop> index loop. You used step to count by more than 1 or count backward, looped through an array, and used break and continue to control execution.

for loops are the right tool whenever you know (or can calculate) how many times you need to repeat something.

What's Next?

The next lesson covers the while loop — repeating a block of code for as long as a condition stays true, useful when you don't know in advance how many iterations you'll need.