Next: , Up: Drivers


2.1.1 Numerical Iteration

— Clause: for var &sequence

The general form for iterating over a sequence of numbers requires a variable and, optionally, one or more keywords that provide the bounds and step size of the iteration. The &sequence lambda-list keyword is a shorthand for these sequence keywords. They are: from, upfrom, downfrom, to, downto, above, below and by. from provides the starting value for var and defaults to zero. to provides a final value and implies that the successive values of var will be increasing; downto implies that they will be decreasing. The loop terminates when var passes the final value (i.e. becomes smaller or larger than it, depending on the direction of iteration); in other words, the loop body will never be executed for values of var past the final value. below and above are similar to to and downto, except that the loop terminates when var equals or passes the final value.

If no final value is specified, the variable will be stepped forever. Using from or upfrom will result in increasing values, while downfrom will give decreasing values.

On each iteration, var is incremented or decremented by the value of the sequence keyword by, which defaults to 1. It should always be a positive number, even for downward iterations.

In the following examples, the sequence of numbers generated is shown next to the clause.

       (for i upfrom 0) => 0 1 2 ...
       (for i from 5) => 5 6 7 ...    ; either from or upfrom is okay
       (for i downfrom 0) => 0 -1 -2 ...
       (for i from 1 to 3) => 1 2 3
       (for i from 1 below 3) => 1 2
       (for i from 1 to 3 by 2) => 1 3
       (for i from 1 below 3 by 2) => 1
       (for i from 5 downto 3) => 5 4 3