Adobe ColdFusion 8

Using while loops

The while loop has the following format:

while (expression) statement

The while statement does the following:

  1. Evaluates the expression.
  2. If the expression is True, it does the following:
    1. Executes the statement, which can be a single semicolon-terminated statement or a statement block in curly braces.
    2. Returns to step 1.

If the expression is False, processing continues with the next statement.

The following example uses a while loop to populate a 10-element array with multiples of five.

a = ArrayNew(1);
loop = 1;
while (loop LE 10) {
    a[loop] = loop * 5;
    loop = loop + 1;
    }

As with other loops, you must make sure that at some point the while expression is False and you must be careful to check your index arithmetic.