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 ...")
 
Line 2: Line 2:
  
 
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 loop, [[statement]]s are repeated forever.
  
  
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
  
 
  while true do
 
  while true do
Line 15: Line 15:
  
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
  
 
  repeat
 
  repeat
Line 23: Line 23:
  
  
== [[Break]] statement ==
+
== [[Break|<syntaxhighlight lang="pascal" enclose="none">Break</syntaxhighlight>]] statement ==
  
"[[While]] [[True]] [[Do]]" or "[[Repeat]] [[Until]] [[False]]" loops looks infinite at first glance,  
+
"[[While|<syntaxhighlight lang="pascal" enclose="none">While</syntaxhighlight>]] [[True|<syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>]] [[Do|<syntaxhighlight lang="pascal" enclose="none">do</syntaxhighlight>]]" or "[[Repeat|<syntaxhighlight lang="pascal" enclose="none">repeat</syntaxhighlight>]] [[Until|<syntaxhighlight lang="pascal" enclose="none">until</syntaxhighlight>]] [[False|<syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>]]" loops looks infinite at first glance,  
but there may be a way to escape the loop through [[Break]].
+
but there may be a way to escape the loop through <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight>.
  
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
  
 
var
 
var
Line 44: Line 44:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
var
 
var
 
   i:integer;
 
   i:integer;
Line 59: Line 59:
  
 
== See also ==
 
== See also ==
* [[True]]
+
* [[True|<syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>]]
* [[False]]
+
* [[False|<syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>]]
* [[Repeat]] [[Until]]
+
* [[Repeat|<syntaxhighlight lang="pascal" enclose="none">repeat</syntaxhighlight>]] [[Until|<syntaxhighlight lang="pascal" enclose="none">until</syntaxhighlight>]]
* [[While]] [[Do]]
+
* [[While|<syntaxhighlight lang="pascal" enclose="none">while</syntaxhighlight>]] [[Do|<syntaxhighlight lang="pascal" enclose="none">do</syntaxhighlight>]]
* [[Break]]
+
* [[Break|<syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight>]]

Revision as of 16:00, 1 July 2019

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 loop, statements are repeated forever.


 while true do
   begin
   end;


 repeat
 until false;


Break statement

"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.


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;

See also