Difference between revisions of "REPEAT..UNTIL"

From Free Pascal wiki
m (rewrite in more idiomatic English)
m (extended with example suggested by sieben)
Line 15: Line 15:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
A <tt>repeat</tt> loop encloses its executed statements, which means they do not need to be further enclosed in a <tt>begin ... end<tt> block. Note that a repeat loop continues until its controlling Boolean expression is <tt>TRUE</tt>; whereas the <tt>while</tt> loop continues until its Boolean expression is <tt>FALSE</tt>.
+
A <tt>repeat</tt> loop encloses its executed statements, which means they do not need to be further enclosed in a <tt>begin ... end<tt> block. Note that a repeat loop continues until its controlling Boolean expression is <tt>True</tt>; whereas the <tt>while</tt> loop continues until its Boolean expression is <tt>False</tt>.
  
Use a <tt>REPEAT</tt> loop when the looping statement(s) must execute at least once, whatever the initial value of the controlling Boolean condition.
+
For instance, the following <tt>repeat</tt> loop executes at least once:
 +
<syntaxhighlight lang=pascal>
 +
repeat
 +
  WriteLn(Node.Text);
 +
  Node := GetNextNode;
 +
until not Assigned(Node);
 +
</syntaxhighlight>
 +
 
 +
It assumes that Node is not Nil at the outset. If this assumption is incorrect, the code will fail, and the program may crash.
 +
 
 +
A <tt>while</tt> loop is more defensive, since the needed check is performed before any loop statements are executed:
 +
<syntaxhighlight lang=pascal>
 +
while Assigned(Node) do
 +
  begin
 +
    WriteLn(Node.Text);
 +
    Node := GetNextNode;
 +
  end;
 +
</syntaxhighlight>
 +
 
 +
Use a <tt>repeat</tt> loop when the looping statement(s) must execute at least once, whatever the initial value of the controlling Boolean condition.
  
 
{{TYNavigator|WHILE..DO|FOR..IN}}
 
{{TYNavigator|WHILE..DO|FOR..IN}}

Revision as of 21:49, 20 November 2020

English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

 ◄   ▲   ► 

REPEAT...UNTIL

The repeat .. until construct is termed a post-test loop, because the controlling condition is tested after each iteration of the loop.

It has the following syntax:

repeat
  statement1;
  // statement2;
  // further statements...
until BooleanExpression;

A repeat loop encloses its executed statements, which means they do not need to be further enclosed in a begin ... end block. Note that a repeat loop continues until its controlling Boolean expression is True; whereas the while loop continues until its Boolean expression is False.

For instance, the following repeat loop executes at least once:

repeat
  WriteLn(Node.Text);
  Node := GetNextNode;
until not Assigned(Node);

It assumes that Node is not Nil at the outset. If this assumption is incorrect, the code will fail, and the program may crash.

A while loop is more defensive, since the needed check is performed before any loop statements are executed:

while Assigned(Node) do
  begin
    WriteLn(Node.Text);
    Node := GetNextNode;
  end;

Use a repeat loop when the looping statement(s) must execute at least once, whatever the initial value of the controlling Boolean condition.

 ◄   ▲   ►