The while statement repeats its code while a specified condition is true. while is not standard in Scheme or Common Lisp, but is found in many languages. Its format is:
      (while condition code )
The condition is tested first; if it is true (anything other than #f), then the code is executed, and the operation is repeated. The value of while is #[undefined].
while can be used to attempt to solve a problem, stopping when a solution is reached. Of course, this has the danger of an infinite loop if no solution is found.
; sqrt using while
(define (mysqrt x)
(let ((estimate 1.0))
(while (> (abs (- (square estimate) x))
1.0e-8)
(set! estimate
(/ (+ estimate
(/ x estimate))
2)) )
estimate))
Contents    Page-10    Prev    Next    Page+10    Index