Difference between revisions of "REPEAT..UNTIL"

From Lazarus wiki
(Use pascal highlighter)
m (rewrite in more idiomatic English)
Line 2: Line 2:
 
{{TYNavigator|WHILE..DO|FOR..IN}}
 
{{TYNavigator|WHILE..DO|FOR..IN}}
  
REPEAT..UNTIL (author: Tao Yue, state: unchanged)
+
REPEAT...UNTIL
  
The posttest loop has the following format:
+
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>
 
<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.
+
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 22:35, 20 November 2020

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

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

 ◄   ▲   ►