Free Pascal supports the For loop construction. A for loop is used in case one wants to calculate
something a fixed number of times. The prototype syntax is as follows:
Here, Statement can be a compound statement. When the For statement is encountered, the
control variable is initialized with the initial value, and is compared with the final value. What
happens next depends on whether to or downto is used:
- In the case To is used, if the initial value is larger than the final value then Statement
will never be executed.
- In the case DownTo is used, if the initial value is less than the final value then
Statement will never be executed.
After this check, the statement after Do is executed. After the execution of the statement, the
control variable is increased or decreased by 1, depending on whether To or Downto is used. The
control variable must be an ordinal type, no other types can be used as counters in a
loop.
Remark
- Free Pascal always calculates the upper bound exactly once before initializing the
counter variable with the initial value.
- It is not allowed to change (i. e. assign a value to) the value of a loop variable inside
the loop.
- the loop can be exited using the break system routine.
- the loop can be forced to the next iteration (if any) using the continue system
routine.
- The value of the loop variable is undefined after a loop has completed or if a loop is not
executed at all. However, if the loop was terminated prematurely with an exception or
a break or goto statement, the loop variable retains the value it had when the loop
was exited.
- if the loop variable is the Result variable, and the loop is ended with exit, the last
value before exit will be retained.
- For nested procedures, a loop variable must be a local variable. If you declare a loop
variable outside the nested procedure where the loop is, the compiler will complain. It
is however allowed to use a global variable in a procedure.
- The compiler does not expliticly forbid jumping with a goto statement into a for loop
block, but doing so will result in unpredictable behaviour.
The following are valid loops:
For Day := Monday to Friday do Work;
For I := 100 downto 1 do
WriteLn (Counting down : ,i);
For I := 1 to 7*dwarfs do KissDwarf(i);
The following will generate an error:
For I:=0 to 100 do
begin
DoSomething;
I:=I*2;
end;
because the loop variable I cannot be assigned to inside the loop.
The following will also generate an error:
program test;
{$ifdef fpc}
{$mode delphi}
{$h+}
{$endif}
procedure Proc;
var
i: integer;
procedure Nested;
begin
for i := 1 to 2 do ;
end;
begin
end;
begin
end.
because the variable I is not defined in Nested and it’s not a global variable either.
But the following will compile:
program test;
{$ifdef fpc}
{$mode delphi}
{$h+}
{$endif}
var
i: integer;
procedure Nested;
begin
for i := 1 to 2 do ;
end;
begin
end.
Inside the loop statement the Break and Continue system routines can be used to jump to the
end or just after the end of the For block statement: in the case of Continue, this means that the
for loop condition is again evaluated. In the case of nested loops, only the innermost loop is
considered. Note that Break and Continue are not reserved words and therefore can be
overloaded.