Difference between revisions of "Repeat"

From Free Pascal wiki
Jump to navigationJump to search
(clean up syntax)
(refactor)
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
{{Repeat}}
 
{{Repeat}}
  
The [[Reserved word|reserved word]] {{HL|repeat}} is used as a [[loop instruction]] where the test occurs after the loop is executed. It is a control construct that is similar to a [[While|{{HL|while}}]]-[[Do|{{HL|do}}]] loop.
+
This [[Reserved word|reserved words]] <syntaxhighlight lang="pascal" inline>repeat</syntaxhighlight> in conjunction with <syntaxhighlight lang="pascal" inline>until</syntaxhighlight> are used to create tail-controlled [[Loops|loops]].
  
==Syntax==
+
== syntax ==
 +
A tail-controlled loops start with <syntaxhighlight lang="pascal" inline>repeat</syntaxhighlight>, followed by a possibly empty list of [[statement]]s, and concluded by <syntaxhighlight lang="pascal" inline>until</syntaxhighlight> and a <syntaxhighlight lang="pascal" inline>Boolean</syntaxhighlight> [[expression]].
  
 +
The following ([[Infinite loop|infinite]]) loops demonstrate the syntax:
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
  repeat
+
// empty loop body is legal:
    <statement block>
+
repeat
  until <condition>;
+
until false;
 +
</syntaxhighlight>
 +
Note, <syntaxhighlight lang="pascal" inline>repeat</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>until</syntaxhighlight> already form a frame in their own right.
 +
You do not need to surround your statements by an extra [[Begin|<syntaxhighlight lang="pascal" inline>begin</syntaxhighlight>]]&nbsp;…&nbsp;[[End|<syntaxhighlight lang="pascal" inline>end</syntaxhighlight>]]-frame.
 +
In fact, you can put as many sequences (also called ''compound statements'') between <syntaxhighlight lang="pascal" inline>repeat</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>until</syntaxhighlight> as you want:
 +
<syntaxhighlight lang="pascal">
 +
repeat
 +
begin
 +
write('x');
 +
end;
 +
begin
 +
write('o');
 +
end;
 +
until false;
 +
</syntaxhighlight>
 +
Note, it is not necessary, but allowed to put a [[;|semicolon]] prior <syntaxhighlight lang="pascal" inline>until</syntaxhighlight>:
 +
<syntaxhighlight lang="pascal">
 +
repeat
 +
write('zZ')
 +
until false;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
* <statement block>: One or more [[Pascal]] [[statement]]s. The {{HL|repeat}}-{{HL|until}} construct is a [[Block|block]] statement, so a [[Begin|{{HL|begin}}]]-[[End|{{HL|end}}]] statement block is not required.
+
== semantics ==
* <condition>: [[expression|Expression]] that evaluates to a [[Boolean|<syntaxhighlight lang="pascal" enclose="none">boolean</syntaxhighlight>]] value.
+
Since the loop “head” appears at the tail, the loop body is executed at least once and the loop condition evaluated at the ''end'' of every iteration.
 
+
If the condition evaluates to [[false and true|<syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>]], another iteration occurs.
==Example==
 
  
 +
Therefore, <syntaxhighlight lang="pascal" inline>repeat</syntaxhighlight>&nbsp;…&nbsp;<syntaxhighlight lang="pascal" inline>until</syntaxhighlight> loops are particularly useful to ensure a certain sequence of statements is run ''at least'' once.
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
  x := 1;
+
repeat
  repeat
+
write('Enter a positive number: ');
    DoSomethingHere(x);
+
readLn(i);
    x := x + 1;
+
  until x = 10;
+
// readLn loads a default value if the source is EOF.
 +
// For integer values the default is zero.
 +
// Since our loop condition requires _positive_ values,
 +
// this loop would be stuck _indefinitely_ if EOF(input).
 +
// Ergo, we check for that:
 +
if eof(input) then
 +
begin
 +
writeLn;
 +
writeLn(stdErr, 'error: input has reached EOF');
 +
halt(1);
 +
end;
 +
until i > 0;
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
The user will be prompted again and again, but ''at least once'', until he finally enters a positive number.
 +
 +
Another standard usage example is the ''reverse'' Horner scheme as demonstrated in [[Base converting]].
  
 +
== see also ==
 +
* [[While|<syntaxhighlight lang="pascal" inline>while</syntaxhighlight>]] for ''head''-controlled loops
 +
* [[Break|<syntaxhighlight lang="pascal" inline>break</syntaxhighlight>]]
 +
* [[Continue|<syntaxhighlight lang="pascal" inline>continue</syntaxhighlight>]]
 +
* Object Pascal Tutorial on [[REPEAT..UNTIL|<syntaxhighlight lang="pascal" inline>repeat…until</syntaxhighlight>]]
  
 
{{Keywords}}
 
{{Keywords}}
 +
[[Category: Code]]

Revision as of 00:30, 21 November 2020

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

This reserved words repeat in conjunction with until are used to create tail-controlled loops.

syntax

A tail-controlled loops start with repeat, followed by a possibly empty list of statements, and concluded by until and a Boolean expression.

The following (infinite) loops demonstrate the syntax:

// empty loop body is legal:
repeat
until false;

Note, repeat and until already form a frame in their own right. You do not need to surround your statements by an extra begin … end-frame. In fact, you can put as many sequences (also called compound statements) between repeat and until as you want:

repeat
begin
	write('x');
end;
begin
	write('o');
end;
until false;

Note, it is not necessary, but allowed to put a semicolon prior until:

repeat
	write('zZ')
until false;

semantics

Since the loop “head” appears at the tail, the loop body is executed at least once and the loop condition evaluated at the end of every iteration. If the condition evaluates to false, another iteration occurs.

Therefore, repeat … until loops are particularly useful to ensure a certain sequence of statements is run at least once.

repeat
	write('Enter a positive number: ');
	readLn(i);
	
	// readLn loads a default value if the source is EOF.
	// For integer values the default is zero.
	// Since our loop condition requires _positive_ values,
	// this loop would be stuck _indefinitely_ if EOF(input).
	// Ergo, we check for that:
	if eof(input) then
	begin
		writeLn;
		writeLn(stdErr, 'error: input has reached EOF');
		halt(1);
	end;
until i > 0;

The user will be prompted again and again, but at least once, until he finally enters a positive number.

Another standard usage example is the reverse Horner scheme as demonstrated in Base converting.

see also


Keywords: begindoelseendforifrepeatthenuntilwhile