Go to the first, previous, next, last section, table of contents.

FOR Statement

FOR <id> ":=" <expr1> TO <expr2> [ BY <stepval> ] DO <stmts> END

Initializes <id> to <expr1>, evaluates <expr1> and <expr2>, then executes <stmts>, increments <id> by <stepval> (by 1 if <stepval> is not specified), and loops if <id> <= <expr2>. If <stepval> is negative, then loops while <id> >= <expr2>.

<expr1> and <expr2> must both be of the same ordinal type (see section Ordinal Types). <id> is defined locally by the FOR loop as a READONLY variable, and its type is the type common to <expr1> and <expr2>. For example:

VAR i: INTEGER := 5;
    x: INTEGER;
BEGIN
  FOR i := 1 TO 10 DO  x := i;  END; (* FOR *)
  IO.PutInt(i);   (* Puts '5', because the 'i' in the FOR loop isn't the same 
                     as this 'i' *)
  IO.PutInt(x);   (* Puts '10' because 'x' was assigned the value of the 'i'
                     in the FOR loop *)
END

Another example prints the letters 'A'..'E' using the CHAR ordinal type for the FOR limits:

FOR i := 'A' TO 'E' DO IO.PutChar(i); END  (* 'i' is of type CHAR *)


Go to the first, previous, next, last section, table of contents.