Next: , Previous: , Up: Iteration Control   [Contents][Index]


15.25.6.2 ’for-as-arithmetic’

This is the first of seven ‘for/as’ syntaxes.

for var [type-spec]
        [{from | downfrom | upfrom} expr1]
        [{to | downto | upto | below | above} expr2]
        [by expr3]
 as var [type-spec] [{from | downfrom | upfrom} expr1]
        [{to | downto | upto | below | above} expr2]
        [by expr3]

The for or as construct iterates from the value specified by ‘expr1’ to the value specified by ‘expr2’ in increments or decrements denoted by ‘expr3’. Each expression is evaluated only once and must evaluate to a number.

The variable var is bound to the value of expr1 in the first iteration and is stepped by the value of expr3 in each succeeding iteration, or by 1 if expr3 is not provided.

Prepositions

The following loop keywords serve as valid prepositions within this syntax. At least one of these prepositions must be used with this syntax.

from

The loop keyword ‘from’ marks the value from which stepping begins, as specified by expr1. Stepping is incremental by default. For decremental stepping, use ‘above’ or ‘downto’ with expr2. For incremental stepping, the default ‘from’ value is 0.

downfrom, upfrom

The loop keyword ‘downfrom’ indicates that the variable var is decreased in decrements specified by expr3; the loop keyword ‘upfrom’ indicates that var is increased in increments specified by expr3.

to

The loop keyword ‘to’ marks the end value for stepping specified in expr2. Stepping is incremental by default. For decremental stepping, use ‘downto’, ‘downfrom’, or ‘above’ with expr2.

downto, upto

The loop keyword ‘downto’ allows iteration to proceed from a larger number to a smaller number by the decrement expr3. The loop keyword ‘upto’ allows iteration to proceed from a smaller number to a larger number by the increment expr3. Since there is no default for expr1 in decremental stepping, you must supply a value with ‘downto’.

below, above

The loop keywords ‘below’ and ‘above’ are analogous to ‘upto’ and ‘downto’, respectively. These keywords stop iteration just before the value of the variable var reaches the value specified by expr2; the end value of expr2 is not included. Since there is no default for expr1 in decremental stepping, you must supply a value with ‘above’.

by

The loop keyword ‘by’ marks the increment or decrement specified by expr3. The value of expr3 can be any positive number. The default value is 1.

In an iteration control clause, the ‘for’ or ‘as’ construct causes termination when the specified limit is reached. That is, iteration continues until the value var is stepped to the exclusive or inclusive limit specified by expr2.

exclusive range

The range is exclusive if expr3 increases or decreases var to the value of expr2 without reaching that value;

the loop keywords ‘below’ and ‘above’ provide exclusive limits.

inclusive range

An inclusive limit allows var to attain the value of expr2;

to’, ‘downto’, and ‘upto’ provide inclusive limits.

A common convention is to use ‘for’ to introduce new iterations and ‘as’ to introduce iterations that depend on a previous iteration specification.17

Examples

;;; Print some numbers.

(loop as i from 1 to 5
      do (print i)) ;Prints 5 lines
;;; Print every third number.

(loop for i from 10 downto 1 by 3
      do (print i)) ;Prints 4 lines
;;; Step incrementally from the default starting value.

(loop as i below 5
      do (print i)) ;Prints 5 lines

Footnotes

(17)

However, loop does not enforce this convention, and some of the examples below violate it. De gustibus non disputandum est.-GLS


Next: ’for-as-in-list’, Previous: Examples of ’for-as’ Construct, Up: Iteration Control   [Contents][Index]