Home > Articles > Mobile Application Development & Programming > HTML5 & Web Standards

Using CFScript

  • Print
  • + Share This
From the author of

From the author of

Looping

You can create several different types of loops within a script block. Just as you specify different attributes of the <CFLOOP> tag within CFML's tag-based syntax, loop statements take different elements when used inside cfscript.

If you have worked with Java at all, you will be pleased to see that the syntax is almost exactly the same for many of these loops.

for Loop

A for loop will iterate as long as a given condition returns true. The code has the following structure: Specify for and then the put three loop elements inside parentheses. The loop elements are separated with semicolons. The first specifies an initiator, which will run before the loop iterates. The second element specifies the condition to test against, and the final loop element specifies the loop step.

To illustrate, let us output the numbers 1 to 10 to the browser (see Listing 5).

Listing 5: A for Loop

<cfscript>

// output the loop index in each iteration

for (idx = 1; idx lte 10; idx = idx +1) {

   WriteOutput("#idx#<br>");
}
</cfscript>

We start with a variable declaration (idx = 1), which initializes the counter. Then we specify the condition to test for (loop as long as the idx value is less than or equal to 10). Then we add 1 to the counter variable each time the loop iterates. Finally, we output the current value of idx and an HTML <br> so that each loop outputs on its own line. The following is output to the browser:

1
2
3
4
5
6
7
8
9
10

You may have thought that it would output the numbers 1 to 11. However, the third element in the loop runs only after the condition has evaluated to true. The loop will terminate the instant the condition is false and therefore won't output.

while Loops

The while loop is similar to using the <CFLOOP condition = "expression"> tag. It loops as long as the expression is true. The statement will therefore not execute at all if the expression specified is false to begin with. It looks like Listing 6.

Listing 6: while Loop

<cfscript>
   idx = 1; 

   while (idx lte 10) {

   WriteOutput("#idx#<br>");

   idx = idx + 1;
}

</cfscript>

The output to the browser is this:

1
2
3
4
5
6
7
8
9
10

Set the variable to 1, and then loop once while the variable is less than or equal to 10. Immediately after we output to the browser, we add 1 to the current idx. When the loop goes to iterate again, idx has the new value, which is retested. The loop stops when the condition is false.

do-while Loop

A do-while loop is the same as a while loop, except for the following distinction: A do-while loop tests the condition after each iteration of the loop—not before. (See Listing 7.)

Listing 7: do-while Loop

<cfscript>

idx = 1;

do {
   WriteOutput("#idx#<br>");
   
   idx = idx +1;
   }
while (idx lte 10);

</cfscript>

Here is the output:

1
2
3
4
5
6
7
8
9
10

for-in Loops

A for-in loop is the final item to examine in CFScript. for-in loops execute one time for each element in a ColdFusion structure variable. This is the script version of writing <CFLOOP collection = "#StructName#">. This can be a very useful loop in CFScript because ColdFusion stores much of its internal data as structures (for instance, COOKIE variables and APPLICATION, and SESSION variables are stored as structures). Let's create a new structure and loop over it with for-in (see Listing 8).

Listing 8: A Collection Loop

<cfscript>
// create the structure

strProducts = StructNew();

// populate the structure

strProducts.Name = "Core ColdFusion 5";
strProducts.Price = "44.95";
strProducts.ISBN = "0119299377";

// loop over it and output the key-value pair

for (idx in strProducts) {

   WriteOutput("#idx# is #strProducts[idx]#<br>");
}
</cfscript>

Using CFScript is a fast-performing and intuitive way to write in ColdFusion. Now with ColdFusion 5 you can write user-defined functions, which requires your knowledge of regular ColdFusion functions and CFScript. Enjoy!

  • + Share This
  • 🔖 Save To Your Account