Difference between revisions of "Basic Pascal Tutorial/Chapter 3/REPEAT..UNTIL"

From Free Pascal wiki
Jump to navigationJump to search
m (Text replace - "delphi>" to "syntaxhighlight>")
m (bypass language bar/categorization template redirect [cf. discussion])
 
(10 intermediate revisions by 6 users not shown)
Line 1: Line 1:
REPEAT..UNTIL (author: Tao Yue, state: unchanged)
+
{{Basic Pascal Tutorial/Chapter 3/REPEAT..UNTIL}}
 +
{{TYNavigator|Chapter 3/WHILE..DO|Chapter 3/FOR..IN}}
  
The posttest loop has the following format:
+
REPEAT...UNTIL
<syntaxhighlight>
+
 
 +
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:
 +
<syntaxhighlight lang=pascal>
 
repeat
 
repeat
 
   statement1;
 
   statement1;
   statement2
+
   // statement2;
 +
  // further statements...
 
until BooleanExpression;
 
until BooleanExpression;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
In a <tt>repeat</tt> loop, compound statements are built-in -- you don't need to use begin-end. Also, the loop continues until the Boolean expression is <tt>TRUE</tt>, whereas the <tt>while</tt> loop continues until the 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>.
  
This loop is called a posttest loop because the condition is tested after the body of the loop executes. The <tt>REPEAT</tt> loop is useful when you want the loop to execute at least once, no matter what the starting value of the Boolean expression is.
+
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.
 +
 
 +
It is not difficult to inadvertently write a Boolean expression controlling a repeat loop that never becomes True. This gives rise to an infinite or endless loop, and causes the program to hang.
 +
 
 +
One programming style deliberately sets up an infinite loop, and inserts a <tt>Break</tt> or <tt>Exit</tt> instruction controlled by some condition evaluated in the middle of the loop to break out of the otherwise infinite loop:
 +
<syntaxhighlight lang=pascal>
 +
repeat
 +
  statement1;
 +
  if Condition then
 +
    Break;
 +
  statement_that_might_affect_the_Condition;
 +
until False;
 +
</syntaxhighlight>
 +
Successful use of this style depends on Condition dependably becoming True, without exception, at some point. Otherwise you have built in an inescapable infinite loop.
  
{|style=color-backgroud="white" cellspacing="20"
+
{{TYNavigator|Chapter 3/WHILE..DO|Chapter 3/FOR..IN}}
|[[WHILE..DO|previous]] 
 
|[[Contents|contents]]
 
|[[FOR..IN|next]]
 
|}
 

Latest revision as of 15:19, 20 August 2022

български (bg) 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.

It is not difficult to inadvertently write a Boolean expression controlling a repeat loop that never becomes True. This gives rise to an infinite or endless loop, and causes the program to hang.

One programming style deliberately sets up an infinite loop, and inserts a Break or Exit instruction controlled by some condition evaluated in the middle of the loop to break out of the otherwise infinite loop:

repeat
  statement1;
  if Condition then
    Break;
  statement_that_might_affect_the_Condition;
until False;

Successful use of this style depends on Condition dependably becoming True, without exception, at some point. Otherwise you have built in an inescapable infinite loop.

 ◄   ▲   ►