ZetCode

Pascal loops

last modified November 18, 2026 Free Pascal 3.2.2

A loop repeats a statement or a block of statements. Pascal has three loop forms: the counted for loop, the condition-first while loop, and the condition-last repeat loop. Free Pascal also provides for..in over strings, arrays, and other enumerable values. This article shows each form, the Break and Continue procedures, nested loops, and the old goto statement.

For to

A for loop runs its body once for every value in a range. The range starts at the first expression, ends at the second, and moves upward by one when the keyword is to. The loop variable must be an ordinal variable declared before the loop.

forto.pas
program forto;
{$mode objfpc}{$H+}

var
  i, total: Integer;

begin
  total := 0;
  for i := 1 to 5 do
  begin
    total := total + i;
    WriteLn('i = ', i, '  total = ', total);
  end;
  WriteLn('after loop, i = ', i);
end.

The body is a compound statement between begin and end, because it contains two statements. The loop adds each value of i to total and prints the running sum. When the loop finishes normally, the control variable keeps the last value it had, so i is 5 after the loop.

$ fpc -gl forto.pas
$ ./forto
i = 1  total = 1
i = 2  total = 3
i = 3  total = 6
i = 4  total = 10
i = 5  total = 15
after loop, i = 5

For downto

The keyword downto counts downward. The start expression is evaluated first, then the bound, and the loop subtracts one after each iteration. As with to, the body runs at least once only when the start value is already within the range; if the start is below the bound in a downto loop, the body does not run at all.

fordownto.pas
program fordownto;
{$mode objfpc}{$H+}

var
  i: Integer;

begin
  for i := 5 downto 1 do
    Write(i, ' ');
  WriteLn;
  WriteLn('after loop, i = ', i);
end.

The loop prints the numbers from 5 down to 1, each followed by a space, and the empty WriteLn ends the line. After a normal downto loop the control variable holds the lower bound, which is 1 here. A for loop does not count one past the end in either direction.

$ fpc -gl fordownto.pas
$ ./fordownto
5 4 3 2 1 
after loop, i = 1

Bounds and the control variable

The two bounds of a for loop are evaluated once, before the first iteration. Changing a variable that was used as a bound inside the body does not change how many times the loop runs. The control variable is managed by the loop and should not be assigned inside the body.

forbounds.pas
program forbounds;
{$mode objfpc}{$H+}

var
  i, last, count: Integer;

begin
  last := 5;
  count := 0;
  for i := 1 to last do
  begin
    last := 1;
    Inc(count);
  end;
  WriteLn('iterations = ', count);
  WriteLn('i after loop = ', i);
end.

The assignment last := 1 inside the body does not shorten the loop, because the bound was already read as 5. The loop runs five times and i ends at 5. If the bound must be recomputed on every iteration, use a while loop instead.

$ fpc -gl forbounds.pas
$ ./forbounds
iterations = 5
i after loop = 5

While

A while loop tests its condition before every iteration. If the condition is false at the start, the body never runs. The body must change something that affects the condition, or the loop will not terminate.

whileloop.pas
program whileloop;
{$mode objfpc}{$H+}

var
  n, digits: Integer;

begin
  n := 407;
  digits := 0;
  while n > 0 do
  begin
    n := n div 10;
    Inc(digits);
  end;
  WriteLn('digits = ', digits);
end.

The loop counts the decimal digits of 407 by dividing by 10 until the number becomes zero. Each division removes one digit, and digits counts the divisions. The condition is checked again after every iteration, so the loop stops as soon as n reaches zero.

$ fpc -gl whileloop.pas
$ ./whileloop
digits = 3

Repeat until

A repeat loop tests its condition after the body, so the body runs at least once. The body is not wrapped in begin and end; the keywords repeat and until do that job. The loop continues while the condition is false and stops when it becomes true, which is the opposite of while.

repeatloop.pas
program repeatloop;
{$mode objfpc}{$H+}

var
  n: Integer;

begin
  n := 1;
  repeat
    Write(n, ' ');
    n := n * 2;
  until n > 20;
  WriteLn;
end.

The program prints the powers of two from 1 to 16. After 16 is printed, n becomes 32, the condition n > 20 is true, and the loop stops. If n started at 100, the body would still run once and print 100 before the condition was tested.

$ fpc -gl repeatloop.pas
$ ./repeatloop
1 2 4 8 16 

For in

A for..in loop visits every value of a string, an array, a set, or another enumerable value. The loop variable takes each element in turn, so the body does not need an index. The variable must be assignment-compatible with the element type.

forin.pas
program forin;
{$mode objfpc}{$H+}

var
  c: Char;
  a: array[1..5] of Integer = (2, 4, 6, 8, 10);
  x, total: Integer;

begin
  for c in 'Pascal' do
    Write(c, ' ');
  WriteLn;
  total := 0;
  for x in a do
    total := total + x;
  WriteLn('total = ', total);
end.

The first loop visits the six characters of the string literal, and the second visits the five integers of the static array. The array is declared with the values in parentheses, a form the arrays article explains. The sum of the array elements is 30.

$ fpc -gl forin.pas
$ ./forin
P a s c a l 
total = 30

Break and Continue

Break leaves the innermost loop immediately. Continue skips the rest of the current iteration and starts the next one. Both are procedures in the System unit, but they are used like statements. They work in for, while, and repeat loops.

breakcontinue.pas
program breakcontinue;
{$mode objfpc}{$H+}

var
  i: Integer;

begin
  for i := 1 to 10 do
  begin
    if i = 3 then
      Continue;
    if i = 6 then
      Break;
    Write(i, ' ');
  end;
  WriteLn;
end.

The value 3 is skipped, so the output starts with 1 and 2. When i reaches 6, Break leaves the loop before Write runs, so 6 and everything after it is missing. The result is 1 2 4 5. The final WriteLn still runs, because it is outside the loop.

$ fpc -gl breakcontinue.pas
$ ./breakcontinue
1 2 4 5 

Nested loops

A loop can contain another loop. The inner loop runs to completion for every iteration of the outer loop, and Break or Continue affects only the innermost loop that contains it. Leaving both loops needs a flag or a goto.

nested.pas
program nested;
{$mode objfpc}{$H+}

var
  i, j: Integer;

begin
  for i := 1 to 3 do
  begin
    for j := 1 to 3 do
    begin
      if j = 2 then
        Break;
      Write('(', i, ',', j, ') ');
    end;
  end;
  WriteLn;
end.

The inner loop normally runs three times, but Break stops it when j is 2. Each outer iteration therefore prints only the pair with j = 1. The outer loop continues, so the output has three pairs.

$ fpc -gl nested.pas
$ ./nested
(1,1) (2,1) (3,1) 

Goto and labels

Pascal still has the goto statement and labels declared in a label section. A label is an identifier followed by a colon, and goto transfers control to it. Structured loops are preferred, but goto is occasionally used to leave several nested loops at once or to jump to a cleanup block.

gotoloop.pas
program gotoloop;
{$mode objfpc}{$H+}

label
  Again;

var
  i: Integer;

begin
  i := 1;
Again:
  Write(i, ' ');
  Inc(i);
  if i <= 3 then
    goto Again;
  WriteLn;
end.

The label Again marks the point where the loop body starts. After printing and incrementing, the program jumps back while i is at most 3. The same effect is written more clearly with a while loop, and modern Pascal style reserves goto for exceptional cases.

$ fpc -gl gotoloop.pas
$ ./gotoloop
1 2 3 

Infinite loops

while True do is the usual way to write a loop that runs until something inside it stops the loop. The body must contain a Break, an Exit, or a call to Halt; otherwise the program never ends. A server loop and a menu loop are common examples.

Inside a procedure, Exit leaves the procedure and therefore also leaves the loop. In the main block, Exit ends the program with status zero. The procedures article shows the procedure form.

Next steps

The numeric functions article uses loops and accumulators for numerical work. The procedures article packages a loop in a function, and the arrays article shows how loops visit array elements by index.

Source

Free Pascal Reference guide

Free Pascal: control statements

Pascal on Wikipedia

In this article we have used for, downto, while, repeat, and for..in loops, Break and Continue, nested loops, and goto. The series continues with Pascal numeric functions.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pascal tutorials.