Page One

Campus Computing News

Free Virus Protection for Home PCs

The Force is better and stronger than ever!

Remedy Enhanced: Introducing Rem-Mail

Today's Cartoon

RSS Matters

SAS Corner

The Network Connection

Link of the Month

WWW@UNT.EDU

Short Courses

IRC News

Staff Activities

Subscribe to Benchmarks Online
    

WWW@UNT.EDU


The Quest for ColdFusion: Loop-Da-Loop

By Shannon Eric Peevey, UNT Central Web Support

Hello, all! Hope that the world is treating you ok, and that this article finds you happy and full of joy :) I know that the last month has been a rare opportunity to partake of the many joys of life... Vacations, classes, and opportunities to help you all out with your ColdFusion problems.From setting up sessions, to modifying queries for a new database server, you all have been involved in many of the exciting aspects of Central Web Support, or the "edge of technology", as I like to think of it. ;) Though many of you have very specific questions about ColdFusion programming, I want to continue this month with our look at Control Structures, and in particular, <CFLOOP>s.

What are they...?

If you remember in last month’s article, the <CFIF> control structures allowed us to perform a test on a condition. Basically, if x where TRUE, then perform this action, “else if” x where FALSE, perform another predefined action. <CFLOOP>s are like <CFIF> statements in the fact that they allow us to define a course of action based on a set of conditions. The difference is that we are not setting up “branch”, or choice, actions, but setting up recurring, or iterating actions. Therefore, while the condition is TRUE, repeat this action over and over again. This type of control structure is very nice, because it allows us to write repetitive actions in a few short lines, instead of the many lines of code it would take to perform multiple actions on a certain variable without the loop. A simple example would be a program that counts from one to ten. Check this out:

<!--- set the variable for our testing condition --->
<CFSET #x# =1>
 
<!--- open the loop and set the condition --->
<CFLOOP CONDITION=”#x# LTE 10”>
 
<!--- this is the action that is repeated --->
Interesting… The variable #x# keeps growing!
It is now equal to <CFOUTPUT>#x#</CFOUTPUT>
<CFSET #x# = #x# + 1>
 
<!--- let’s close the loop now --->
</CFLOOP>

This code, when parsed by our ColdFusion server, will set a variable, “#x#”, to the value of 1, enter the loop and add one to the previous value of “#x#” until “#x#” reaches the value 10, at which time the code will exit the loop and precede to the next bit of code.

Let’s get a little more in depth…

Though the short explanation found in the previous paragraph adequately explains the actions to a person that is familiar with loops, perhaps a deeper explanation of each line would be more appropriate for the user that is unfamiliar with these concepts. The first line: 

<CFSET #x# =1>

introduces another tag to us. The <CFSET> tag is used to set the variable value to a beginning, or default value. This actually performs a very important function. Before we can continue, we need to talk about what a variable is exactly. A variable is a “placeholder” that our programming languages use, to set aside a place in the memory of a computer. Once this placeholder has been created, we are allowed to place any value in that “place in memory”. (This placeholder is really an easy mnemonic device that allows humans to think in human terms. The actual placeholder is a group of numbers that point to an address in the memory of the computer. This would be difficult for humans to program with, so programming languages now allow us to use alphanumeric names for variables, and the computer does the work of associating that name with the address in memory. Good for us!! ) The placeholder holds an address in memory that contains the last value held in that address. What is this value? We have no way of knowing. Therefore, when we initialize, (meaning setting the beginning value of a variable), the variable, we are placing a known value into that “memory address”. Voila! We need no longer worry about the junk values that might have used that memory address in the past. (Very important!) So, the statement:

<CFSET #x#=1>

initializes the placeholder, “#x#”, to 1. Therefore, we know that the value that is associated with our variables “place in memory” is equal to 1.

What about the loop?

Now, let’s look at the <CFLOOP> that we have created.

<CFLOOP CONDITION=”#x# LTE 10”>
 
Your actions here
</CFLOOP>

The first line opens the loop, and then allows us to choose which type of loop we would like to use. (Unlike most programming languages, ColdFusion has a set of five predefined loops that we can use. These are index, conditional, list, and query loops. (That’s only four, you say? There is a loop that allows us to iterate over a COM structure, but I will not actually be touching on this type of loop in these articles.) This article is dealing with the conditional <CFLOOP>.) The type of loop is identified by the attribute given to the tag. (In this case, CONDITION.) The CONDITION attribute is then given a value, which is the condition that we are testing for. In our example, we are setting the loop to true while the variable “#x#” is less than or equal to 10. Therefore, the loop will iterate 10 times. (If we increment the variable “#x#” by one each time through the loop.) We could increment the value of “#x#” by any value, and the loop will iterate the appropriate number of times.

Let’s quiz you… How many iterations will take place if “#x#” is incremented by 2? Five times, right? Why not six…? If you think about it, we start with a value of 1. This meets the conditions of the loop, and executes the code found in the loop. Then 2 is added to the 1, and we have a value of 3, which also meets the condition of “#x# LTE 10”. The code is executed again, and 2 is added to the current value of “#x#”, which is 3, and we now have a value of 5. This meets the condition, and the code is executed again. (The third time.) 2 is added to “#x#” and we now have 7. Condition met. Code executed, add 2 to 7 and we have 9. Condition met, (the fifth time), code executed and 2 is added to 9 to equal 11. We now check the value of “#x#” against the condition, and the value does not meet the condition, we, therefore, break from the loop and continue executing the rest of the code though out the page.

Now, what about this code that I have been talking about being executed in the loop… Let’s look at the code found in our example:

<CFLOOP CONDITION=”#x# LTE 10”>
 
<!--- this is the action that is repeated --->
Interesting… The variable #x# keeps growing!
It is now equal to <CFOUTPUT>#x#</CFOUTPUT>
<CFSET #x# = #x# + 1>

 </CFLOOP>

(I have added the open and close of the <CFLOOP> to help you visualize what is going on.) The indented code is the code that is executed every time the conditional test of the variable value returns TRUE. What is happening? First, we are printing some text to the screen. That text is:

Interesting… The variable #x# keeps growing!
It is now equal to <CFOUTPUT>#x#</CFOUTPUT>

The <CFOUTPUT> tags allow us to output the current value of “#x#” to the screen. The current value of the “#x#”, on the first iteration, is 1. It is then incremented by one, and will print to the screen as:

Interesting… The variable #x# keeps growing! It is now equal to 1
 
Interesting… The variable #x# keeps growing! It is now equal to 2
 
Interesting… The variable #x# keeps growing! It is now equal to 3
 
Etc, etc…
 
Interesting… The variable #x# keeps growing! It is now equal to 10

When the number increments to 11, the condition becomes false and the loop is exited.

But, how is the variable value incremented by one? The magic to that question is found in the fourth line of indented code, “<CFSET #x# = #x# + 1>”. As you can see, we see our new friend, the <CFSET> tag. What does the <CFSET> tag do again? That’s right, it sets the value of our variable, or “placeholder”. In this case, it is taking the current value of “#x#” and adds one to it. It then places that value in the variable “#x#” and returns to the condition statement to be tested again, repeating until the condition returns false and the loop is exited…

Here is the source code for a finished page that you can cut and paste and test on your test servers:

<html>
<head>
<title> This is my First ColdFusion Application that uses a loop </title>
</head>
<body>
<h1 align="center"> This Page Will Use a Loop </h1>
<h1 align="center"> to Count to 10!!! </h1>
<p align="left">____________________________________________________________</p>
<!--- set the variable for our testing condition --->
<CFSET #x#=1>
 
<!--- open the loop and set the condition --->
CFLOOP CONDITION="#x# LTE 10">
 
<!--- this is the action that is repeated --->
<p> Interesting… The variable #x# keeps growing!
It is now equal to <CFOUTPUT>#x#</CFOUTPUT></p>
<CFSET #x# = #x# + 1>
 
<!--- let’s close the loop now --->
</CFLOOP>
<p align="left">____________________________________________________________</p>
<p align="left"></p>
<DIV ALIGN=CENTER>
<p align="left"></p>
<p align="left"></p>
</div>
</form>
</body>
</html>

or, if you would just like to see the code in action, click here.

In Conclusion...

And that’s all there is to it. <CFLOOPS> are that powerful, and can save you hours of coding. They can be used to drop changing values into code, and can be used to pull values from an array, or “list” as they call them. These exciting pieces of code will have you sitting in front of your Linux workstations for hours, I know it. And, if you get some new ideas about some great ways to use them, feel free to send them to me and I will highlight a few of them in upcoming articles. Until next month, when we will explore some of the other types of loops that are available to you…

Peace!