# while -- while loops

## Synopsis

• Usage:
while p list x do z
• Consequences:
• The expression p is repeatedly evaluated. As long as the result is true, x is evaluated and its value is saved, and z is evaluated and its value is discarded. When the value of p is false, then the loop terminates, and the list of values of x is returned as the value of the entire expression.

## Description

 i1 : i = 0 ; while i < 10 list i^2 do i = i+1 o2 = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81} o2 : List

The list x clause may be omitted, in which case no list is accumulated, and null is returned as the value of the expression.

 i3 : i = 0 ; while i < 4 do (print i; i = i+1) 0 1 2 3

Alternatively, the do z clause may be omitted.

 i5 : i = 0 ; while i < 10 list (i = i+1; i^2) o6 = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100} o6 : List

Observe the use of the semicolon (see ;) in the expression above.

If continue is executed by x then execution of x is interrupted, no value is added to the list, and iteration of the loop continues.

 i7 : i = 0 ; while i < 10 list (i = i+1; if odd i then continue; i^2) o8 = {4, 16, 36, 64, 100} o8 : List

If continue w is executed by x then execution of x is interrupted, the value of w is added to the list, and iteration of the loop continues.

 i9 : i = 0 ; while i < 10 list (i = i+1; if odd i then continue 1234; i^2) o10 = {1234, 4, 1234, 16, 1234, 36, 1234, 64, 1234, 100} o10 : List

If break v is executed by x, then the loop is stopped and v is returned as its value.

 i11 : i = 0 ; while i < 10 list (i = i+1; if i == 5 then break i; i^2) o12 = 5

If break is executed by x, then the loop is stopped and the list accumulated so far is returned as the value.

 i13 : i = 0 ; while i < 10 list (i = i+1; if i == 5 then break; i^2) o14 = {1, 4, 9, 16} o14 : List

## For the programmer

The object while is .