Basic Pascal Tutorial/Chapter 3/REPEAT..UNTIL
Sent here from REPEAT..UNTIL.
English · Français · Български · 中文(中国) · 日本語
| ◄ | ▲ | ► |
|---|
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.
| ◄ | ▲ | ► |
|---|
From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/REPEAT..UNTIL", as the Internet Archive kept it on 2025-04-03, last changed on 2025-03-01. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Edited on wiki.freepascal.org by 8 people, 2007–2022
From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.
| When | Who | What changed |
|---|---|---|
| Kees | New page: REPEAT..UNTIL The posttest loop has the following format: <font color="#006699"><strong>repeat</strong></font> statement1<font color="#000000"><strong>;</strong></font> statement2 ... | |
| Kees | ||
| Kees | ||
| Kees | ||
| Kees | ||
| Kees | ||
| Vincent | Text replace - "delphi>" to "syntaxhighlight>" (minor) | |
| Jwdietrich | ||
| Swen | (minor) | |
| FTurtle | ||
| FTurtle | ||
| Bart | Use pascal highlighter | |
| HowardPC | rewrite in more idiomatic English (minor) | |
| HowardPC | extended with example suggested by sieben (minor) | |
| HowardPC | Added Note about infinite loops (minor) | |
| Kai Burghardt | Kai Burghardt moved page REPEAT..UNTIL to Basic Pascal Tutorial/Chapter 3/REPEAT..UNTIL: tidy up main name space: create subpage hierarchy for basic Pascal tutorial [cf. [[Special: PermaLink/149778#Clutt... (minor) | |
| Kai Burghardt | bypass redirects [cf. discussion] | |
| Kai Burghardt | bypass language bar/categorization template redirect [cf. discussion] (minor) |
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
