A do...while loop is almost identical to a while loop, with one important difference: it checks its condition after the loop body runs, not before. That guarantees the loop body executes at least once, even if the condition is false from the very start.
This is useful whenever you need to do something first and only decide afterward whether to repeat it — like showing a menu once, then asking "do that again?"
Learning Objectives
After completing this lesson, you'll be able to:
- Write a do...while loop in CFScript.
- Explain the difference between while and do...while.
- Simulate a do...while loop in tag syntax, since <cfloop> doesn't have a built-in equivalent.
Basic do...while Loop
It Always Runs at Least Once
The clearest way to see the difference: start the condition out already false.
while checks first and skips the body entirely if the condition starts false. do...while runs the body once, then checks.
Simulating do...while in Tag Syntax
This is worth knowing honestly: <cfloop> only has a pre-condition loop (the condition attribute is checked at the top of each pass, like a while loop) — there's no built-in tag for a post-condition loop. To get do...while behavior in tag syntax, start a flag variable as true so the first pass always runs, then set it based on the real condition at the end of the loop body.
keepGoing starts true regardless of the real condition, guaranteeing the first pass runs — exactly matching do...while's behavior.
Real-World Example: Input Validation Retry
A common do...while use case: try something once, then keep retrying only if it failed.
Common Beginner Mistakes
Assuming <cfloop> has a native do...while attribute
It doesn't — condition is always checked before each pass, never after. Trying to find a "post-condition" attribute will send you looking for something that isn't there; use the flag-variable pattern shown above instead.
Forgetting the semicolon after while(...) in CFScript
do { ... } while (condition) without a trailing semicolon is a syntax error in CFScript — unlike a plain while loop, do...while needs it.
Using do...while when a while loop was actually needed
If it would be wrong for the code to ever run when the condition starts false, use while, not do...while — do...while always executes at least once no matter what.
Best Practices
- Only reach for do...while when you genuinely need the body to run at least once.
- In tag syntax, name your flag variable something readable like keepGoing or shouldRetry, not just x.
- Keep the real exit condition clearly visible at the end of the loop body, not buried in the middle.
Interview Questions
What's the key difference between while and do...while?
while checks its condition before the loop body runs, so the body may never execute. do...while checks after, so the body always runs at least once.
Does <cfloop> have a built-in do...while equivalent?
No — its condition attribute is always checked before each pass, like a while loop. A do...while has to be simulated with a flag variable that starts true.
What happens if you forget the semicolon after a CFScript do...while?
It's a syntax error — do { ... } while (condition); requires the trailing semicolon, unlike a plain while loop.
Summary
In this lesson, you learned CFScript's do...while loop, which always runs its body at least once before checking the condition — the opposite order from a while loop. You also saw how to simulate that behavior in tag syntax, since <cfloop> has no native post-condition loop type.
What's Next?
The next lesson covers the enhanced loop (for...in) — a shorter way to loop over arrays, structs, lists, and query results without manually managing an index counter.