While


Purpose

"While" is used to create loops.

Usage

"While" takes two arguments, a test and an expression. As long as the test continues to evaluate to "true", "while" will continue evaluating the expression. If you would like to evaluate several expressions inside the while loop, then you should use "begin".

Examples

Here we'll count to five.
  ox --> (define num 1)  
  1
  ox --> (while (<= num 5)
           (begin
              (print num)
              (set! num (+ num 1))
          ))
  1
  2
  3
  4
  5
  #f
Now we'll do the same thing, but instead of printing numbers, we'll create a world, with a sphere moving and changing color over time:
  (set! frame 0)
  (while (< frame 30)
    (begin
      (world
       (camera "main" "perspective" 'from (vec3 1 1 3) 'to (vec3 0 0 0) 'fov 45)
       (color (lerp (vec3 1 0 0) (vec3 0 1 0) (/ frame 29)))
       (translate (lerp (vec3 -2 0 0) (vec3 2 0 0) (/ frame 29)))
       (sphere))
      (set! frame (+ frame 1))
      ))


Return to Scheme Introduction
mrl