<cfwhile>
Simplification of the tag cfloop-condition, analog to the cfscript "while" loop.
This tag must have a body.
This tag is also supported within <cfscript>
<cfwhile
condition=boolean
label=string
><!--- body ---></cfwhile>
Attribute | Description |
---|---|
condition
boolean, optional
|
condition o the expression |
label
string, optional
|
used to address this loop from a "break" or "continue" statement (instead of the nearest one). |
Examples
testCondition = true;
cnt = 0;
while(testCondition) {
cnt = cnt + 1;
if (cnt EQ 5) {
testCondition = false;
}
}
echo(cnt);
<!--- testing condition --->
<cfset testCondition = true>
<cfset cnt = 0>
<cfloop condition="#testCondition#">
<cfset cnt = cnt + 1>
<cfif cnt EQ 5>
<cfset testCondition = false>
</cfif>
</cfloop>
<cfoutput>#cnt#</cfoutput>
// breaking out using a label
x = 0;
WhileLabel: while (x < 10){
writeOutput("x is #x#<br>");
switch (x){
case 1:
break;
case 3:
break WhileLabel;
}
x++;
writeOutput("end of loop<br>");
}
writeOutput("After loop, x is #x#<br>");
<!--- breaking out using a label --->
<cfset x = 0>
<cfloop condition="x LT 10" index="i" label="WhileLabel">
<cfoutput>x is #x#<br></cfoutput>
<cfswitch expression="#x#">
<cfcase value="1">
<cfbreak>
</cfcase>
<cfcase value="3">
<cfbreak loop="WhileLabel">
</cfcase>
</cfswitch>
<cfset x = x + 1>
<cfoutput>end of loop<br></cfoutput>
</cfloop>
<cfoutput>After loop, x is #x#<br></cfoutput>
See also
- <cfbreak>
- <cfloop>
- Search Issue Tracker
- Search Lucee Test Cases (good for further, detailed examples)