for
The For keyword starts a control loop, which is executed a finite number of times.
The Variable is set to the result of the 1st Expression. If the result is less than
or equal to the result of the 2nd Expression (when to is specified), then the Statement
is executed. Variable is then incremented by 1 and the process is repeated until the
variable value exceeds the 2nd expression value.
For downto, the variable value is checked as being greater than or equal to the 2nd
expression, and its value is decremented at the loop end.
The Expressions may result in any Ordinal type - Integers, Characters or Enumerations.
The Statement maybe a single line, or a set of statements within a begin/end block.
Note: The loop variable is not guaranteed to contain the finishing value once
the loop completes.
Example
Download This Source for Free Pascal
var
i : Integer;
c : char;
suit : (Hearts, Clubs, Diamonds, Spades);
begin
// Loop 5 times
for i := (10 div 2) downto 1 do Writeln('i = ',i);
// Loop 5 times
for c := 'E' downto 'A' do Writeln('c = ',c);
// Loop 3 times
for suit := Diamonds DownTo Hearts do
Writeln('Suit = ',Ord(suit));
end.
Output
i = 5
i = 4
i = 3
i = 2
i = 1
c = E
c = D
c = C
c = B
c = A
Suit = 2
Suit = 1
Suit = 0
See Also
Begin,
Do,
Downto,
End,
Repeat,
To,
Until,
While.