Macromedia coldfusion 4.5-cfml language reference User Manual
130
CFML Language Reference
FROM="1" TO="5">
The loop index is <CFOUTPUT>#LoopCount#</CFOUTPUT>.<BR>
</CFLOOP>
The result of this loop in a browser looks like this:
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.
The loop index is 2.
The loop index is 3.
The loop index is 4.
The loop index is 5.
In this example, the STEP value has a default value of 1. But you can set the STEP value
to change the way the INDEX value is incremented. The following code counts
backwards from 5:
to change the way the INDEX value is incremented. The following code counts
backwards from 5:
<CFLOOP INDEX="LoopCount"
FROM="5"
TO="1"
STEP="-1">
TO="1"
STEP="-1">
The loop index is <CFOUTPUT>#LoopCount#</CFOUTPUT>.<BR>
</CFLOOP>
The result of this loop in a browser looks like this:
The loop index is 5.
The loop index is 4.
The loop index is 3.
The loop index is 2.
The loop index is 1.
The loop index is 4.
The loop index is 3.
The loop index is 2.
The loop index is 1.
Conditional Loops
A conditional loop iterates over a set of instructions while a given 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
commonly known as WHILE loops, as in "loop WHILE this 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
commonly known as WHILE loops, as in "loop WHILE this condition is true. "
Syntax
<CFLOOP CONDITION="expression">
CONDITION
Required. Sets the condition that controls the loop. The loop will repeat as long as
the condition evaluates as TRUE. When the condition is FALSE, the loop stops.
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. The results
look exactly like the Index loop example.
look exactly like the Index loop example.
<!—- Set the variable CountVar to 0 —->
<CFSET CountVar=0>
<!—- Loop until CountVar = 5 —->
<CFLOOP CONDITION="CountVar LESS THAN OR EQUAL TO 5">
<CFSET CountVar=0>
<!—- Loop until CountVar = 5 —->
<CFLOOP CONDITION="CountVar LESS THAN OR EQUAL TO 5">