# DELPHI / Loop

// show 1,2,3,4,5 message boxes
var i: integer;
begin
i := 1 to 5 do
begin
ShowMessage(‘Box: ‘+IntToStr(i));
end;
end;

var i: integer;
begin
for i := 5 downto 1 do
begin
ShowMessage(‘T minus ‘ + IntToStr(i) + ‘seconds’);
end;
ShowMessage(‘For sequence executed!’);
end;

var i,j: integer;
begin
//be aware:
//this double loop is executed 4×4=16 times
for i:= 1 to 4 do
for j:= 4 downto 1 do
ShowMessage(‘Box: ‘+
IntToStr(i)+ ‘,’ +
IntToStr(j));
end;

repeat
begin
statements;
end;
until condition = true

while condition = true do
begin
statements;
end;

RTL Reference|Glossary|Tips/Tricks|FREE App/VCL|Best’O’Net|Books| Back

LOOPS
Delphi For Beginners:
Repeating operations in Object Pascal.
Dateline: 08/10/99
while language = Delphi do
begin
Use(language);
end;

Loops
The loop is a common element in all programming languages. Object Pascal has three control structures that execute blocks of repeatedly: for, repeat-until and while-do.

The FOR loop
Suppose we need to repeat an operation a fixed number of times.

// show 1,2,3,4,5 message boxes
var i: integer;
begin
for i := 1 to 5 do
begin
ShowMessage(‘Box: ‘+IntToStr(i));
end;
end;

The value of a control variable (i), which is really just a counter, determines how many times a for statement runs. The keyword for sets up a counter. In the preceding example, the starting value for the counter is set to 1. The ending value is set to 5.
When the for statement begins running the counter variable is set to the starting value. Delphi than checks whether the value for the counter is less than the ending value. If the value is greater, nothing is done (program execution jumps to the line of code immediately following the for loop code block). If the starting value is less than the ending value, the body of the loop is executed (here: the message box is displayed). Finally, Delphi adds 1 to the counter and starts the process again.

Sometimes it is necessary to count backward. The downto keyword specifies that the value of a counter should be decremented by one each time the loop executes (it is not possible to specify an increment / decrement different than one). An example of a for loop that counts backward.

var i: integer;
begin
for i := 5 downto 1 do
begin
ShowMessage(‘T minus ‘ + IntToStr(i) + ‘seconds’);
end;
ShowMessage(‘For sequence executed!’);
end;

It’s important that you never change the value of the control variable in the middle of the loop. Doing so will cause errors.

Nested FOR loops
Writing a for loop within another for loop (nesting loops) is very useful when you want to fill / display data in a table or a grid.

var i,j: integer;
begin
//be aware:
//this double loop is executed 4×4=16 times
for i:= 1 to 4 do
for j:= 4 downto 1 do
ShowMessage(‘Box: ‘+
IntToStr(i)+ ‘,’ +
IntToStr(j));
end;

The rule for nesting for-next loops is simple: the inner loop (j counter) must be completed before the next statement for the outer loop is encountered (i counter). We can have triply or quadruply nested loops, or even more.

Note: Generally, the begin and end keywords are not strictly required, as you can see. If begin and end are not used, the statement immediately following the for statement is considered the body of the loop.

The WHILE and REPEAT loops
Sometimes we won’t know exactly how many times a loop should cycle. What if we want to repeat an operation until we reach a specific goal?

The most important difference between the while-do loop and the repeat-until loop is that the code of the repeat statement is always executed at least once.

The general pattern when we write a repeat (and while) type of loop in Delphi is as follows:

repeat
begin
statements;
end;
until condition = true

while condition = true do
begin
statements;
end;

Here goes the code to show 5 successive message boxes using repeat-until.

var i: integer;
begin
i:=0;
repeat
begin
i:=i+1;
ShowMessage(‘Box:’+IntToStr(i));
end;
until i>5;
end;

var i: integer;
begin
i:=0;
while i<5 do
begin
i:=i+1;
ShowMessage(‘Box:’+IntToStr(i));
end;
end; 