ReadabilityCreating a simple conditional loop in ColdFusion using the CFLOOP tag
In previous examples, “Creating a simple index loop in ColdFusion using the CFLOOP tag” and “Setting the step size on an indexed loop in ColdFusion using the CFLOOP tag”, we saw how you could create a simple index loop in ColdFusion using the <CFLOOP> tag using the index
, from
, and to
, and step
attributes.
The following example shows how you can create a simple conditional loop in ColdFusion using the <CFLOOP> tag using the condition
attribute.
counter == #counter#
The preceding code produces the following output:
counter == 1
counter == 11
counter == 21
counter == 31
counter == 41
The previous example used a condition of LESS THAN, it is important to note that you can instead use the shorthand notation of LT. You could also use a condition of LESS THAN OR EQUAL TO (or use the LTE shorthand), or GREATER THAN (or GT shorthand), or GREATER THAN OR EQUAL TO (or GTE shorthand).
counter == #counter#
counter == #counter#
counter == #counter#
In previous examples, “Creating a simple index loop in ColdFusion using the CFLOOP tag” and “Setting the step size on an indexed loop in ColdFusion using the CFLOOP tag”, we saw how you could create a simple index loop in ColdFusion using the <CFLOOP> tag using the index
, from
, and to
, and step
attributes.
The following example shows how you can create a simple conditional loop in ColdFusion using the <CFLOOP> tag using the condition
attribute.
<cfset counter = 1>
<cfloop condition="counter LESS THAN 50">
<cfoutput>counter == #counter#<br/></cfoutput>
<cfset counter = counter + 10 />
</cfloop> |
<cfset counter = 1>
<cfloop condition="counter LESS THAN 50">
<cfoutput>counter == #counter#<br/></cfoutput>
<cfset counter = counter + 10 />
</cfloop>
The preceding code produces the following output:
counter == 1
counter == 11
counter == 21
counter == 31
counter == 41
The previous example used a condition of LESS THAN, it is important to note that you can instead use the shorthand notation of LT. You could also use a condition of LESS THAN OR EQUAL TO (or use the LTE shorthand), or GREATER THAN (or GT shorthand), or GREATER THAN OR EQUAL TO (or GTE shorthand).
<cfset counter = 1>
<cfloop condition="counter LTE 51">
<cfoutput>counter == #counter#<br/></cfoutput>
<cfset counter = counter + 10 />
</cfloop> |
<cfset counter = 1>
<cfloop condition="counter LTE 51">
<cfoutput>counter == #counter#<br/></cfoutput>
<cfset counter = counter + 10 />
</cfloop>
<cfset counter = 1>
<cfloop condition="counter LESS THAN OR EQUAL TO 51">
<cfoutput>counter == #counter#<br/></cfoutput>
<cfset counter = counter + 10 />
</cfloop> |
<cfset counter = 1>
<cfloop condition="counter LESS THAN OR EQUAL TO 51">
<cfoutput>counter == #counter#<br/></cfoutput>
<cfset counter = counter + 10 />
</cfloop>
<cfset counter = 51>
<cfloop condition="counter GREATER THAN OR EQUAL TO 1">
<cfoutput>counter == #counter#<br/></cfoutput>
<cfset counter = counter - 10 />
</cfloop> |
<cfset counter = 51>
<cfloop condition="counter GREATER THAN OR EQUAL TO 1">
<cfoutput>counter == #counter#<br/></cfoutput>
<cfset counter = counter - 10 />
</cfloop>