- 2.2.9.4. -
Table of Contents
2. TMT Pascal Language Description
2.2. Pascal Language Structure
2.2.9. Statements
2.2.9.4. For Statement
2.2.9.4. For Statement
The for statement allows for repetitive execution of one or more
statements. for executes a loop for a predetermined number of
iterations. for statements take the following form:
for variable := expression to | downto expression do statement
where variable must be of ordinal type. The first expression following variable
is the initial value that is assigned to variable and the second expression
is the limit on the range of values assigned to variable.
Both expressions must be of compatible type.
The to or downto clause specify specifies whether a
variable is incremented or decremented after each iteration of the loop.
If to is specified, the variable is incremented until it hits
the limit of the second expression. downto decrements variable
until it reaches the lower limit of the second expression.
The following are examples of for loops:
for i := 1 to 100 do
begin
WriteLn(i);
Intarray[i] := i + 4;
end;
for x := 5 downto 2 do
WriteLn(x);
A for loop is not executed if the first expression is greater than
or less then the second expression depending upon whether a to
or downto was specified. For instance the following for loop
is not executed:
for i := 5 to 4 do
WriteLn('Will never output this string!');
For a loop, the index variable must either be global or local to the
procedure to which it belongs.
- 2.2.9.4. -