while bool-expr do statements od;
The while
loop executes the statement sequence statements while the
condition bool-expr evaluates to true
.
First bool-expr is evaluated. If it evaluates to false
execution of
the while
loop terminates and the statement immediately following the
while
loop is executed next. Otherwise if it evaluates to true
the
statements are executed and the whole process begins again.
The difference between the while
loop and the repeat until
loop
(see Repeat) is that the statements in the repeat until
loop are
executed at least once, while the statements in the while
loop are
not executed at all if bool-expr is false
at the first iteration.
If bool-expr does not evaluate to true
or false
an error is
signalled and a break loop (see Break Loops) is entered. As usual you
can leave the break loop with quit;
. If you enter return false;
,
execution continues with the next statement immediately following the
while
loop. If you enter return true;
, execution continues at
statements, after which the next evaluation of bool-expr may cause
another error.
gap> i := 0;; s := 0;; gap> while s <= 200 do > i := i + 1; s := s + i^2; > od; gap> s; 204 # first sum of the first i squares larger than 200
GAP 3.4.4