VMS Help
PASCAL, Statements, FOR
*Conan The Librarian (sorry for the slow response - running on an old VAX)
|
|
The FOR statement is a looping statement that repeats execution
of a statement according to the value of a control variable.
The control variable assumes a value within a specified range or
set. A FOR statement has one of the following forms:
FOR control-variable := initial {TO | DOWNTO} final-value DO
statement
FOR control-variable IN set-expression DO
statement
The 'control-variable' is the name of a previously declared
variable of an ordinal type.
The 'initial-value' and 'final-value' are expressions that form
a range and whose type is assignment compatible with the type of
the control variable.
The 'set-expression' is an expression resulting in a value of
SET type. The base type of the set must be assignment
compatible with the control variable.
The 'statement' is any Compaq Pascal statement that does not
change the value of the control variable.
At run time, the initial and final values or the set expression
is evaluated before the loop body is executed.
The 'TO | DOWNTO' directives determine whether loop iteration
will be incremental or decremental, respectively.
In the TO form, Compaq Pascal checks to see if the value of the
control variable is less than or equal to the final value. If
this condition is met, the control variable takes on the value
of the initial value for the first loop iteration. During
iterations, the control variable increments according to its
data type. Looping ceases when the control variable is greater
than the final value.
In the DOWNTO form, Compaq Pascal checks to see if the value of
the control variable is greater than or equal to the final
value. If this condition is met, the control variable takes on
the value of the initial value for the first loop iteration.
During iterations, the control variable decrements according to
its data type. Looping ceases when the control variable is less
than the final value.
In the set expression form, Compaq Pascal checks to see if the
set expression is not the empty set. If this condition is met,
the control variable takes on the value of one of the members of
the set. Iterations occur for each member of the set; the
selection order of members of the set is undefined. Looping
stops after the loop body executes for each member of the set.
In both the TO and the DOWNTO forms, incrementation of the
control variable depends on its type. For example, values
expressed in type INTEGER increment or decrement in units of 1.
Values expressed in type CHAR increment or decrement in
accordance with the ASCII collating sequence.
After normal termination of the FOR statement, the control
variable does not retain a value. You must assign a new value
to this variable before you use it elsewhere in the program. If
the FOR loop terminates with a GOTO statement, the control
variable retains the last assigned value. In this case, you can
use the variable again without assigning a new value.
Example:
FOR I := 1 TO 10 DO
FOR J := 1 TO 10 DO
A[I,J] := 0;
This example shows how you can nest FOR loops. For each value
of I, the executing program steps through all 10 values of the
array J and assigns the value 0 to each component.
Example:
FOR I IN Set 1 DO
Set2 := Set2 + [I + 1];
This example shows a FOR-IN statement. Set2 is assigned the
successor of each value in Set1.
[legal]
[privacy]
[GNU]
[policy]
[netiquette]
[sponsors]
[FAQ]
Polarhome, production since 1999.
Member of Polarhome portal.