|
ColdFusion Day 38: ================ CHAPTER 12 - LOOPING Loops
provide a mechanism by which you can perform repeated actions without having to
write repeated sections of program code. This concept should already be
familiar. You saw basic loops when you learned how to use CFOUTPUT to
produce output for each row in a query result. In this
chapter you will look at loops in a more general sense. Using the CFLOOP
tag, you can produce a wide range of loops, many of which will be familiar to
those who have experience with other programming languages such as C, Perl or
Java. These loops include Index Loops, Conditional Loops, and Query loops. UNDERSTANDING LOOPS Loops are a
basic part of any programmers vocabulary. They enable programmers to repeat the
same action multiple times without needing to write the same section of program
code multiple times. Loops are
used for several purposes, including the following:
* Repeating a
specific action a set number of times - for instance, printing the same output
exactly 10 times.
* Repeating a
specific action until a given condition holds true - for instance, repeatedly
displaying the value of a given variable until the variable contains a specific
value.
* Repeating a
series of actions for each item in a list or array - for instance, looking at
each element in an array, performing a mathematical formula on the value, and
displaying the results. If you come
from a traditional programming background such as C, Java or Perl, you will be
familiar with a range of commands used to create different types of loops,
including commands such as for, foreach, while and repeat. In ColdFusion
templates, however, all these types of loops are achieved using the CFLOOP
tag. The different flavors of loops are achieved using different attributes and
values for the tag. USING A BASIC LOOP WITH THE CFLOOP TAG The most
basic type of loop is the Index Loop. This is a loop that repeats once for
every value between a start value and an end value. In other words, an Index
Loop counts. For instance, a loop that counts from 1 to 10 and repeats specific
actions for each integer value between 1 and 10 is an index loop. Similarly, a
loop that counts down from 87 to 23 in decrements of 2 (in other words:
87,85,83....27,25,23) and performs specific actions for each value is an index
loop. The name
index loop comes from the loop having an index. An index is a named variable
associated with the loop. For each iteration of the loop, the value is assigned
to the variable and this variable can be used like any other variable for any
of the actions performed in the loop. Without
getting into the specifics of the code, let's consider the basic steps required
to use an index loop to output the times table for the number 7 (we'll do the
first 10 entries in the times table). The steps look something like this:
For each integer between 1 and 10 perform the following
steps.
Assign the current integer to the index variable X.
Multiply 7 by the value contained in the variable X and store the result in the
variable Y.
Output the result stored in Y.
End of loop. The logic
isn't complicated, but it is easy to see how the code is more compact, easier
to write, and even easier to read than it would be without a loop. Without a
loop, the algorithm would look like this:
Multiply 7 by 1 and store the result in the variable Y
Output the result stored in Y
Multiply 7 by 2 and store the result in the variable Y
Output the result stored in Y
Multiply 7 by 3 and store the result in the variable Y
Output the result stored in Y
Multiply 7 by 4 and store the result in the variable Y
Output the result stored in Y
Multiply 7 by 5 and store the result in the variable Y
Output the result stored in Y
Multiply 7 by 6 and store the result in the variable Y
Output the result stored in Y
Multiply 7 by 7 and store the result in the variable Y
Output the result stored in Y
Multiply 7 by 8 and store the result in the variable Y
Output the result stored in Y
Multiply 7 by 9 and store the result in the variable Y
Output the result stored in Y
Multiply 7 by 10 and store the result in the variable Y
Output the result stored in Y Now you
need to consider how to use the CFLOOP tag to create an index
loop. The CFLOOP tag can take many attributes, but the
essential ones for an index loop are:
INDEX
Specifies the name of the index variable for the loop.
FROM
Specifies the starting value for the loop
TO
Specifies the ending value for the loop It should
be fairly clear how this works. To implement the loop for the 7 times table
discussed earlier in this chapter, the CFLOOP tag would look
like this:
<CFLOOP INDEX="X" FROM=1 TO=10>
<CFSET Y=7*X>
<CFOUTPUT>7*#X#=#Y#<BR></CFOUTPUT>
</CFLOOP> (http://www.accc.net.au/sybex/loop1.cfm) Of course,
this code isn't that useful because it performs only the 7 times table and only
for a specific range (from 1 to 10). It should be clear that you could leverage
this code to produce any times table.
<CFLOOP INDEX="X" FROM=1 TO=10>
<CFSET Y = #TABLE# * X>
<CFOUTPUT>#TABLE# * #X# =
#Y#<BR></CFOUTPUT>
</CFLOOP> Here all
you have done is replaced the literal 7 with the variable TABLE, which
represents the times table you are supposed to be calculating. Then, all you
need to do is assign a value to the TABLE variable to specify which times table
you want to view. To produce the 5 times table, the code would look like this:
<CFSET TABLE=5>
<CFLOOP INDEX="X" FROM=1 TO=10>
<CFSET Y = TABLE * X>
<CFOUTPUT>#TABLE# * #X# =
#Y#<BR></CFOUTPUT>
</CFLOOP> http://www.accc.net.au/sybex/loop2.cfm This can be
taken a step further, specifying the number of entries to be specified through
a variable. The following code creates the first 15 entries of the 11 times
table by using a variable to specify the TO value in the CFLOOP
tag:
<CFSET TABLE=11>
<CFSET ENTRIES=15>
<CFLOOP INDEX="X" FROM=1 TO=#ENTRIES#>
<CFSET Y = TABLE * X>
<CFOUTPUT>#TABLE# * #X# =
#Y#<BR></CFOUTPUT>
</CFLOOP> (http://www.accc.net.au/sybex/loop3.cfm) You may
have noticed that the preceding index loops suffer for a major limitation: They
only count upward in increments of 1. Of course, there are likely to be times
when it is necessary to count by different increments. This is where the step
attribute comes into play. The step
attribute specifies the size of the increment for the loop and needs to be used
for any increment other than the default of 1. For instance, to count even
numbers it is necessary to use an increment of 2. To count all even numbers
from 2 to 100, the following CFLOOP tag could be used:
<CFLOOP INDEX="X" FROM=2 TO=100 STEP=2> (http://www.accc.net.au/sybex/loop4.cfm) Similarly,
you could count multiples of 5 to 500 with:
<CFLOOP INDEX="X" FROM=5 TO=500 STEP=5> (http://www.accc.net.au/sybex/loop5.cfm) In addition
to counting by increments other than 1, the STEP attribute can
be used to count down. To do this, just use a negative number as the value of
the STEP attribute. For instance, to count down from 10 to 1,
use the tag: <CFLOOP
INDEX="X" FROM=10 TO=1 STEP=-1> (http://www.accc.net.au/sybex/loop6.cfm) |