Conditional Loop

Description

A conditional loop iterates over a set of instructions while a condition is TRUE. To use this type of loop correctly, the instructions must change the condition every time the loop iterates until the condition evaluates as FALSE. Conditional loops are known as WHILE loops, as in "loop WHILE this condition is true. "

Category

Flow-control tags

Syntax

<cfloop condition = "expression"> 

See also

cfabort, cfbreak, cfexecute, cfexit, cfif    cfelseif    cfelse, cflocation, cfswitch    cfcase    cfdefaultcase, cfthrow, cftry cfcatch

Attributes

Attribute
Description
condition
Required. Sets the condition that controls the loop. The loop repeats as long as the condition evaluates as TRUE. When the condition is FALSE, the loop stops.

Example

The following example increments the parameter "CountVar " from 1 to 5.

<!-- Set the variable CountVar to 0 --> 
<cfset CountVar = 0> 
 
<!-- Loop until CountVar = 5 --> 
<cfloop condition = "CountVar LESS THAN OR EQUAL TO 5"> 

  <cfset CountVar = CountVar + 1> 
  The loop index is <cfoutput>#CountVar#</cfoutput>.<BR> 

</cfloop>

The result of this loop in a browser appears as:

The loop index is 1. 
The loop index is 2. 
The loop index is 3. 
The loop index is 4. 
The loop index is 5.