Difference between revisions of "Infinite loop"

From Free Pascal wiki
Jump to navigationJump to search
(Created page with "{{Infinite loop}} An infinite loop (also known as an endless loop or unproductive loop or a continuous loop) is a loop which never ends. Inside loop, statements are repeated ...")
 
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{Infinite loop}}
 
{{Infinite loop}}
  
An infinite loop (also known as an endless loop or unproductive loop or a continuous loop) is a loop which never ends.
+
An '''infinite loop''' (also known as an endless loop or unproductive loop or a continuous loop) is a loop which never ends.
Inside loop, statements are repeated forever.
+
Inside a loop, [[statement]]s are repeated forever.
 
 
 
 
 
 
<syntaxhighlight>
 
 
 
while true do
 
  begin
 
  end;
 
  
 +
There are two implementations of an infinite loop:
 +
<syntaxhighlight lang="pascal">
 +
while true do
 +
begin
 +
// loop body repeated forever
 +
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
+
<syntaxhighlight lang="pascal">
<syntaxhighlight>
+
repeat
 
+
begin
repeat
+
// loop body repeated forever
until false;
+
end
 
+
until false;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
== [[Break|<syntaxhighlight lang="pascal" inline>Break</syntaxhighlight>]] statement ==
 +
“[[While|<syntaxhighlight lang="pascal" inline>While</syntaxhighlight>]] [[True|<syntaxhighlight lang="pascal" inline>true</syntaxhighlight>]] [[Do|<syntaxhighlight lang="pascal" inline>do</syntaxhighlight>]]” or “[[Repeat|<syntaxhighlight lang="pascal" inline>repeat</syntaxhighlight>]] [[Until|<syntaxhighlight lang="pascal" inline>until</syntaxhighlight>]] [[False|<syntaxhighlight lang="pascal" inline>false</syntaxhighlight>]]” loops look infinite at first glance,
 +
but there is a way to escape the loop through the <syntaxhighlight lang="pascal" inline>break</syntaxhighlight> statement.
  
== [[Break]] statement ==
+
<syntaxhighlight lang="pascal">
 
 
"[[While]] [[True]] [[Do]]" or "[[Repeat]] [[Until]] [[False]]" loops looks infinite at first glance,
 
but there may be a way to escape the loop through [[Break]].
 
 
 
 
 
<syntaxhighlight>
 
 
 
 
var
 
var
 
   i:integer;
 
   i:integer;
Line 41: Line 35:
 
     end;
 
     end;
 
end;
 
end;
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
var
 
var
 
   i:integer;
 
   i:integer;
Line 54: Line 47:
 
   until false;
 
   until false;
 
end;   
 
end;   
 +
</syntaxhighlight>
  
 
+
== Optimization ==
</syntaxhighlight>
+
If you really need an infinite loop, it is better to use <syntaxhighlight lang="pascal" inline>repeat … until false;</syntaxhighlight>, 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 ==
 
== See also ==
* [[True]]
+
* [[True|<syntaxhighlight lang="pascal" inline>true</syntaxhighlight>]]
* [[False]]
+
* [[False|<syntaxhighlight lang="pascal" inline>false</syntaxhighlight>]]
* [[Repeat]] [[Until]]
+
* [[Repeat|<syntaxhighlight lang="pascal" inline>repeat</syntaxhighlight>]] [[Until|<syntaxhighlight lang="pascal" inline>until</syntaxhighlight>]]
* [[While]] [[Do]]
+
* [[While|<syntaxhighlight lang="pascal" inline>while</syntaxhighlight>]] [[Do|<syntaxhighlight lang="pascal" inline>do</syntaxhighlight>]]
* [[Break]]
+
* [[Break|<syntaxhighlight lang="pascal" inline>break</syntaxhighlight>]]
 +
* [[Continue|<syntaxhighlight lang="pascal" inline>continue</syntaxhighlight>]]

Latest revision as of 09:49, 4 January 2023

English (en) suomi (fi) français (fr) русский (ru)

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:

while true do
begin
	// loop body repeated forever
end;
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.

var
  i:integer;
begin
  i := 0;
  while true do
    begin
      i := i + 1;
      if i = 100 then break;
    end;
end;
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