Next: , Previous: , Up: iklib syntaxes loops   [Index]


6.8.7.3 Iterating with the for syntax

Syntax: for (?init ?test ?post) ?body0 ?body
Auxiliary Syntax: continue
Auxiliary Syntax: break

This is an iteration syntax similar to the C language for:

(import (vicare))

(for ((define i 0) (< i 3) (set! i (+ 1 i)))
  (display i)
  (display #\space))
-| 0 1 2

(define i)
(for ((set! i 0) (< i 3) (set! i (+ 1 i)))
  (display i)
  (display #\space))
-| 0 1 2

(for ((begin
        (define i 5)
        (define j 10))
      (positive? i)
      (begin
        (set! i (+ -1 i))
        (set! j (+ -1 j))))
  (display i)
  (display #\space)
  (display j)
  (display #\space))
-| 5 10 4 9 3 8 2 7 1 6

The form ?init can be a sequence of definitions and expressions; it is evaluated first and only once; its return values are discarded. The region of bindings defined by ?init includes the ?test, ?post and ?body forms.

The form ?test must be an expression; it is evaluated at the beginning of each iteration: if the result is true the next iteration takes place, else the loop terminates.

The forms ?body must be expressions; they are evaluated at each iteration when the ?test evaluates to true; the return values are discarded.

The form ?post must be an expression; it is evaluated at the end of each iteration after the ?body forms; the return values are discarded.

The syntax continue can be used in the body to immediately exit from the body and jump directly to the evaluation of ?post.

The syntax break can be used in the body to immediately break out of the loop and return to the continuation of for.


Next: , Previous: , Up: iklib syntaxes loops   [Index]