ColdFusion Day 40:

================

 

NESTING LOOPS

 

Loops offer an additional powerful capability: nesting. Nesting refers to the technique of embedding one loop inside another. For instance, using nesting, you can repeat one loop for each iteration of another loop. This capability has already been visited in Chapter 11, "Getting, Nesting, and Formatting Output," in the context of the CFOUTPUT tag.

 

To better understand how this works, let's consider an extension of our basic times table code:

 

    <CFSET TABLE=11>

    <CFSET ENTRIES=15>

    <CFLOOP INDEX="X" FROM=1 TO=#ENTRIES#>

        <CFSET Y = TABLE * X>

        <CFOUTPUT>#TABLE# * #X# = #Y#<BR></CFOUTPUT>

    </CFLOOP>

 

What would happen if you wanted to output a series of times tables?

 

For instance, what if you wanted to output every times table from 1 times to the 9 times table and output 15 entries for each table?  You could nest two index loops as follows:

 

    <CFSET ENTRIES=15>

    <CFLOOP INDEX="Table" FROM=1 TO=9>

        <CFOUTPUT><H1>The #Table# Times Table</H1></CFOUTPUT>

        <CFLOOP INDEX="X" FROM=1 TO=#Entries#>

        <CFSET Y = TABLE * X>

        <CFOUTPUT>#TABLE# * #X# = #Y#<BR></CFOUTPUT>

    </CFLOOP>

    </CFLOOP>

 

(see this live online at: http://www.accc.net.au/sybex/loop7.cfm)

 

What exactly is happening here? Well, the outer loop (the one with the index variable Table) contains the inner loop (the one with the index variable X). The outer loop iterates once for each times table you want to output. The index variable Table contains the number of the current table you want to output.

 

The inner loop is our familiar loop for outputting the times table specified in a variable called Table. It will run once for each iteration of the outer loop, outputting the appropriate times table based on the value of the table variable.

 

When nesting loops, you can have the duration of the inner loop be dependent on the outer loop. An ideal example of this is a set of nested loops for calculating factorials. A factorial is a series of multiplication based on a specific number. For instance, factorial 4 (written 4!) is the value 4*3*2*1

 

A simple loop can calculate a factorial:

 

    <CFSET FACTORIAL=1>

    <CFLOOP INDEX="X" FROM=#NUMBER# TO=1 STEP= -1>

        <CFSET Factorial = Factorial * X>

    </CFLOOP>

    <CFOUTPUT>#NUMBER#! = #Factorial#</CFOUTPUT>

 

The assumption here is that the number against which to calculate the factorial is stored in the variable number. The result is stored in the variable Factorial.

 

Now, if you want to produce multiple factorials (for instance, for each number from 5 to 15), you need two nested loops:

 

    <CFLOOP INDEX=#Number# FROM=5 TO=15>

    <CFSET FACTORIAL=1>

    <CFLOOP INDEX="X" FROM=#NUMBER# TO=1 STEP= -1>

        <CFSET Factorial = Factorial * X>

    </CFLOOP>

    <CFOUTPUT>#NUMBER#! = #Factorial#</CFOUTPUT>

    </CFLOOP>

 

(see this live online at: http://www.accc.net.au/sybex/loop8.cfm)

 

The important point here is that the inner loop's length is dependent on the value of the index variable of the outer loop (the variable number determines how many times the inner loop will iterate).

 

This ability to adjust the length of one nested loop based on values from another loop raises a danger: The chance of producing endless loops is greater. This is especially true if the inner loop is a conditional loop and the condition is based on the index variable from the outer loop. It is important to think through the logic of your loops when nesting them to be sure to avoid endless loops.

 

NOTE    It is possible to nest loops multiple levels deep. The two level nested loops you have seen here are the most common, but you can often find loops nested three or four levels deep.

 

WHERE DO WE GO FROM HERE?

 

Up to this point, you have learned the basics of using ColdFusion to produce dynamic pages. However, the real interactive power of the web and of ColdFusion to enhance that process with dynamic data comes from HTML forms.

 

In the next chapter you will learn how forms integrate into ColdFusion, allowing the dynamic population of form fields as well as the creation of more sophisticated dynamic forms using the CFFORM tag. The chapter will include a discussion of the special Java from controls included with ColdFusion such as tree controls, grid controls and sliders.

 

In Chapter 14, "Forms," you will look at an important issue: form validation. This chapter addresses a fundamental concern with forms: How can you be sure the data entered by a user meets the criteria you expect?

 

Back   Next