Looping


A loop construct usually consists of the same three elements. First, an expression takes place when the loop begins. Second, a condition is tested for each iteration and signals when the loop will conclude. Finally, an expression takes place when the loop ends. You create these three elements in various ways, using the <cfloop> tag depending on the type of loop you require. In a <cfscript> block, the loop elements also vary depending on type. The different types of loops available to <cfscript> are described in the following sections.

for Loop

A for loop is one of the most common loop constructs. In a for loop, as shown in the following code, the loop iterates as long as the condition remains true:

 <cfscript> // the first loop element (index=1) runs BEFORE the loop // the element (index LT 10) is the condition // the last element (index = index+1) will run AFTER each iteration for(index=1; index LT 10; index = index + 1) // note there is no semicolon //after the loop declaration above {  // curly braces encircle the code that will loop  WriteOutput(index); } WriteOutput("loop is done!"); // the above will output after the loop is finished. </cfscript> 

The preceding code outputs the numbers 1 tHRough 9. The condition in the loop (index LT 10) must be true while the loop iterates. As soon as index is 10, the loop terminates.

while Loop

while loops do not carry instance variables, such as index seen in the for loop example. Rather, they simply break out of the loop when a condition is met, as shown in this example:

 <cfscript> a = 1; while (a LT 10) {  WriteOutput(a);  a = a +1; } </cfscript> 

do-while Loop

The difference between a while loop and a do-while loop is that the condition is tested after each iteration, rather than before:

 <cfscript> a=1; do {  WriteOutput(a);  a = a +1; } while (a LT 10); </cfscript> 

for-in Statement

A for-in statement is used exclusively with structures. The expression tested in a for-in loop checks to see whether a specific key is inside a structure:

 <cfscript> myStruct = structNew(); x=1; mystruct[x]=0; // x must be a variable for the 'FOR' statement to work  for (x in myStruct) {  WriteOutput("this will run because x is 1 and 1 is a key of myStruct."); } </cfscript> 

NOTE

In both conditional and loop constructs, the break and continue statements serve important roles. The break statement exits the entire loop or conditional statement. The continue statement, to be used in loops only, iterates the loop from the beginning and runs any post-loop expressions, as in this example:

 for (index=1; index LT 10; index = index + 1) {  if(index IS 5) continue;  WriteOutput(index); } // this will count from 1 to 9 but SKIP 5! 




Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net