Next: , Up: End-Test Control   [Contents][Index]


15.25.7.1 while-until

while expr
until expr
‘while’

allows iteration to continue until the specified expression expr evaluates to nil. The expression is re-evaluated at the location of the ‘while’ clause.

‘until’

is equivalent to ‘while’ (not expr). If the value of the specified expression is non-nil, iteration terminates.

You can use ‘while’ and ‘until’ at any point in a loop. If a ‘while’ or ‘until’ clause causes termination, any clauses that precede it in the source are still evaluated.

If the ‘while’ or ‘until’ construct causes termination, control is passed to the loop ‘epilogue’, where any ‘finally’ clauses will be executed.

;;; A classic "while-loop".
(loop while (hungry-p) do (eat))
;;; UNTIL NOT is equivalent to WHILE.
(loop until (not (hungry-p)) do (eat))
;;; Collect the length and the items of STACK.
(let ((stack '(a b c d e f)))
  (loop while stack
        for item = (length stack) then (pop stack)
        collect item))
;;; Use WHILE to terminate a loop that otherwise wouldn't
;;; terminate.  Note that WHILE occurs after the WHEN.
(loop for i fixnum from 3
      when (oddp i) collect i
      while (< i 5))