Other Types of Loop ColdFusion Help

Two other types of loops can be created using CFLOOP: collection loops that alls>wyou to loop over COM/OCOM objects and structure loops. COM/OCOM loops will be outlined in Chapter 32 “Including External Objects” and structure loops will be considered when we discuss ColdFusion’s structure functions in Chapter 14.1

Nesting Loops

Loops offer an additional powerful capability: nesting. Nesting refers t~,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 12 “Grouping 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:

<CFLOOP INDEX-‘X’ FROM21 TC-#Entries#>
<CFSET Y – Table * X> .
<CFOUTPUT>#Table# * #x# – #Y#<BR></CFOUTPUT>

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 the 1 times table to the 9 times table and output 15 entries for each table? You could nest two index loops as follows:

<CFSET Entries-1S>
<CFLOOP INDEX-‘Table’ FROM=1 T0=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>

What exactly is happening here? Well the outer loop (the one with the index variable Tab’ e) 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 Tab’ e contains the number of the current table you want to output:

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

It is important to note that 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. Afactorial is a series of multiplication based on a specific number. For instance factorial 4 (written as 4!) is the value 4 * 3 * 2 * 1, and 9! is 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.

A simple loop can calculate a factorial

<CFSET Number-6>
<CFSET Factorial – 1>
<CFLOOP INDEX-‘X’ FROM-tNumbert To-l STEP–1>
<CFSET Factorial – Factorial * X>
<CFOUTPUT>tNumbert! – tFactorialt</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 Factor; a1.

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

<CFSET Factorial – 1>
<CFLOOP INDEX-‘X’ FROM-tNumbert To-l STEP~-1>
<CFSET Factorial – Factorial * X>
<CFOUTPUT>tNumbert! – tFactorialt<BR></CFOUTPUT>

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.

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 Cold Fusion 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 disr..russion of the special Java form controls included with ColdFusion, such as tree controls, grid controls, and sliders.

In Chapter 15, “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?

Posted on November 16, 2015 in Looping

Share the Story

Back to Top
Share This