Looping ColdFusion Help

Loops provide a mechanism that you can use to perform repeated actions without having to write repeated sections of program codes. 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 bask part of any programmer’s 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 while 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-s-for instance looking teach 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 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 loops 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, 81… 27,25,23) and performs specific actions for each value is an index loop

The name index loop comes from the loop having a range of numeric values. An index is a named variable associated with the loop. For each iteration of the loop a new 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:

Multiple 7 by 1 and store the result in the variable Y
Output the result stored in Y
Multiple 7 by 2 and store the result in the variable Y
Output the result stored in Y
Multiple 7 by 3 and store the result in the variable Y
Output the result stored in Y
Multiple 7 by 4 and store the result in the variable Y
Output the result stored in Y
Multiple 7 by 5 and store the result in the variable Y
Output the result stored in Y
Multiple 7 by 6 and store the result in the variable Y
Output the result stored in Y
Multiple 7 by 7 and store the result in the variable Y
Output the result stored in Y
Multiple 7 by 8 and store the result in the variable Y
Output the result stored in Y
Multiple 7 by 9 and store the result in the variable Y
Output the result stored in Y
Multiple 7 by 10 and store the result in the variable Y
Output the result stored in Y

Understanding CFLOOP Basics

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 the following:

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:

<CFLClOP INDEXa’X’ FROM = 1 to 10>

The CFLOOP closing tag is used to specify the end of the code to perform for each iteration of the loop. The result is that our entire 7 times table code looks like this:

<CFLOOP INDEX-‘X’ FROMal To-lO>
<CFSET Y a 7 * X>
<CFOUTPUT>7 * #X# – #Y#<BR></CFOUTPUT>
</CFLOOP>

Of course this code isn’t that useful because it performs only the 7 times table and only for a specified range (from 1 to 10). It should be clear that you could leverage this code to produce any times table

<CFSET TABLE-7>
<CFLOOP INDEX-‘X’ FROM-l ro-n»
<CFSET Y – #TABLE# * X>-
<CFOUTPUT>#TABLE# * #X# – #Y#<BR></CFOUTPUT>
</rr’LOOP>

Here all you have done is replace 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 the following:

<CFSET TABLE-S>
<CFLOOP INDEX-‘X’ FROM-l ro-n»
<CFSET Y – Table * X>
<CFOUTPUT>#Table# * #x# – #Y#<BR></CFOUTPUT>
</CFLOOP>
Similarly, the 12 times table would use the following code:
<CFSET TABLE-l2>
<CFLOOP INDEX-‘X.’ FROM-l ro-u»
<CFSET Y a Table * X>
<CFOUTPUT>#Table# * #x# – #Y#<BR></CFOUTPUT>
</CFLOOP>

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-I!>
<CFSET ENTRIES-IS>
<CFLOOP INDEX-‘X’ FROM-I To-#Entries#>
<CFSET Y – Table * X>
<CFOUTPUT>#Table# * #X# – #Y#<BR></CFOUTPUT>
</CFLOOP>

Setting the Step

You may have noticed that the preceding index loops suffer from a major limitation: They count upward only 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 specfines the size of the increment for the loop and needs to be used for any increment other than the default of l

For instance, to count even numbers, it is necessary to use an increment of 2. To count all even numbers from 2 to laO, the following CFLOOP tag could be used: <CFLOOP INDEX-‘ X’ FROM=2 TO=lOO STEP-2>. Similarly, you could count multiples of 5 from 5 to 500 with <CFlOOP INDEX- n X· FROM-S To-SOO STEP-S>.

In addition to counting up by increments other than I, 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 I, use the tag <CFLOOP INDEX=”X” FROM”lO ro-i STEP–D.

Posted on November 16, 2015 in Looping