The do-while loop is like a while loop, except that it tests the loop condition after executing the loop statement block. The do-while loop has the following format:
do statement while (expression);
The do while statement does the following:
If the expression is False, processing continues with the next statement.
The following example, like the while loop example, populates a 10-element array with multiples of 5:
a = ArrayNew(1); loop = 1; do { a[loop] = loop * 5; loop = loop + 1; } while (loop LE 10);
Because the loop index increment follows the array value assignment, the example initializes the loop variable to 1 and tests to make sure that it is less than or equal to 10.
The following example generates the same results as the previous two examples, but it increments the index before assigning the array value. As a result, it initializes the index to 0, and the end condition tests that the index is less than 10.
a = ArrayNew(1); loop = 0; do { loop = loop + 1; a[loop] = loop * 5; } while (loop LT 10);
The following example loops through a query:
<cfquery ... name="myQuery"> ... sql goes here... </cfquery> <cfscript> if (myQuery.RecordCount gt 0) { currRow=1; do { theValue=myQuery.myField[CurrRow]; currRow=currRow+1; } while (currRow LTE myQuery.RecordCount); } </cfscript>