MAXScript Language Reference > Controlling Program Flow >

# While and Do Loops

while and do loops are used to repeatedly execute an expression until a test expression evaluates as false . The while loop and do loop are simple variants of one another.

The syntax is:

    do <expr> while <expr>

    while <expr> do <expr>

Both loops repeat the do <expr> as long as the while <expr> evaluates to the value true . The do form evaluates the do <expr> at least once, evaluating the test expression at the end of each loop. The while form evaluates the test expression at the start of each loop and may not loop at all.

Both forms return as their value the result of the do <expr> from the last successful loop iteration. The while form will return the value undefined if the test expression immediately returns false .

EXAMPLES:
    while x > 0 do
    ( print x
    x -= 1
    )
    while not eof f do print (read_value f)
    do
    (
    exchanged=false
    for i=1 to imax do
    (
    j=i+gap
    if (compare array[j] array[i]) do
    (swap array[j] array[i]; exchanged=true)
    )
    )
    while exchanged

#### See Also

- Skipping Loop Iterations

- Loop Exit