Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/REPEAT..UNTIL: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11REPEAT...UNTIL
22
33The repeat .. until construct is termed a *post*-test loop, because the controlling condition is tested *after* each iteration of the loop.
44
55It has the following syntax:
66
77``` pascal
88repeat
99 statement1;
1010 // statement2;
1111 // further statements...
1212until BooleanExpression;
1313```
1414
15-A `repeat` 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 `True`; whereas the `while` loop continues until its Boolean expression is `False`.
15+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`.
1616
1717For instance, the following `repeat` loop executes at least once:
1818
1919``` pascal
2020repeat
2121 WriteLn(Node.Text);
2222 Node := GetNextNode;
2323until not Assigned(Node);
2424```
2525
2626It assumes that Node is not Nil at the outset. If this assumption is incorrect, the code will fail, and the program may crash.
2727
2828A `while` loop is more defensive, since the needed check is performed before any loop statements are executed:
2929
3030``` pascal
3131while Assigned(Node) do
3232 begin
3333 WriteLn(Node.Text);
3434 Node := GetNextNode;
3535 end;
3636```
3737
3838Use a `repeat` loop when the looping statement(s) must execute at least once, whatever the initial value of the controlling Boolean condition.
3939
4040It 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.
4141
4242One 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:
4343
4444``` pascal
4545repeat
4646 statement1;
4747 if Condition then
4848 Break;
4949 statement_that_might_affect_the_Condition;
5050until False;
5151```
5252
5353Successful use of this style depends on Condition dependably becoming True, without exception, at some point. Otherwise you have built in an inescapable infinite loop.
5454
5555---
5656*From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/REPEAT..UNTIL", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. 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](/wiki-contact).*

Back to history