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 .


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

Creative Commons License Except where otherwise noted, this work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License