Sign in Join
Wiki copytemporary

Infinite loop

English · Français · Suomi · Русский

An infinite loop (also known as an endless loop or unproductive loop or a continuous loop) is a loop which never ends. Inside a loop, statements are repeated forever.

There are two implementations of an infinite loop:

pascal
while true do
begin
    // loop body repeated forever
end;
pascal
repeat
begin
    // loop body repeated forever
end
until false;

Break statement

“While true do” or “repeat until false” loops look infinite at first glance, but there is a way to escape the loop through the break statement.

pascal
var
  i:integer;
begin
  i := 0;
  while true do
    begin
      i := i + 1;
      if i = 100 then break;
    end;
end;
pascal
var
  i:integer;
begin
  i := 0;
  repeat
    i := i + 1;
    if i = 100 then break;
  until false;
end;

Optimization

If you really need an infinite loop, it is better to use repeat … until false;, since it shifts all instructions of the body “less” to the right (at least, if there is more than one statement in the loop).

See also


From the Free Pascal wiki, page "Infinite loop", as it stood on 2023-01-04 after a change by Paskal, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.