Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

numbers

nicric edited this page · 2 revisions

Previous Strings --- Next Recordsets


5. Numbers

There are two basic kinds of numbers in CFML: integers (whole numbers) and real (numbers with a decimal point). For our workshop, we'll only be dealing with integers. You can use normal math operations with integers including +, -, / and *. The ++ operator can be used to increment a number. It is also the only one we will use to control a loop. We will talk more about Conditional Looping in section 9. Try out this example for the ++ operator:

Tag Syntax

<cfset loop = 0 />
<cfoutput>
 <cfloop condition="loop LT 5" >
  #loop# Hello, world!<br />
  <cfset loop++ />
 </cfloop>
 I am here!<br />
</cfoutput>

Script Syntax

<cfscript>
loop = 0; 
while (loop < 5) { 
 WriteOutput("#loop# Good Morning! ");
 WriteOutput("...is it lunch time yet?<br />");
 loop++;
}
</cfscript>

In this next example we're using the cfloop instruction with a multiple instructions inside the condition. The CFML script syntax looks for the starting { and the ending }. Each instruction between the beginning {and ending } will be executed if the condition is true.

In the tag example there's no need to manage the index inside the loop if you're simply stepping through one item at a time. You can use the from and to arguments, and ColdFusion will simply loop from the first value to the second, and automatically increment the variable in the index argument.

Try this example with multiple instructions:

Tag Syntax

<cfset loop = 0 />
<cfoutput>
<cfloop index="loop" from = "0" to = "4">
 #loop# Good Morning!
 ...is it lunch time yet?<br />
</cfloop>
</cfoutput>

Script Syntax

<cfscript>
for (loop = 0 ; loop < 5 ; loop++)
 WriteOutput("#loop# Hello, world!<br />");
WriteOutput("I am here<br />");
</cfscript>

It's also possible to go through a loop and step over more than one value at a time. The following examples will step through the loop and increase the "loop" index by two for each time through the loop.

Tag Syntax

<cfset loop = 0 />
<cfoutput>
<cfloop index="loop" from="0" to="4" step="2">
 #loop# Good Morning!
 ...is it lunch time yet?<br />
</cfloop>
</cfoutput>

Script Syntax

<cfscript>
loop = 0;
while (loop < 5) {
 WriteOutput("#loop# Good Morning! ");
 WriteOutput("...is it lunch time yet?<br />");
 loop++;
}
</cfscript>

Previous Strings --- Next Recordsets

Something went wrong with that request. Please try again.