Loops

A loop is a construct that enables you to repeatedly perform some type of action. Pascal's loop constructs are very similar to what you should be familiar with from your experience with other languages, so we don't spend any time teaching you about loops. This section describes the various loop constructs you can use in Pascal.

The for Loop

A for loop is ideal when you need to repeat an action a predetermined number of times. Here's an example, albeit not a very useful one, of a for loop that adds the loop index to a variable 10 times:

var
  I, X: Integer;
begin
  X := 0;
  for I := 1 to 10 do
    inc(X, I);
end.

The C equivalent of the preceding example is as follows:

void main(void) {
  int x, i;
  x = 0;
  for(i=1; i<=10; i++)
    x += i;
}

Here's the ...

Get Borland® Delphi™ 6 Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.