Basic Pascal Tutorial/Chapter 3/REPEAT..UNTIL: what changed
Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 1 | + | [[Basic Pascal Tutorial/Chapter 3/WHILE..DO|◄]] | [[Basic Pascal Tutorial/Contents|▲]] | [[Basic Pascal Tutorial/Chapter 3/FOR..IN|►]] | |
| 2 | + | ---|---|--- | |
| 3 | + | ||
| 4 | + | REPEAT...UNTIL | |
| 4 | 5 | ||
| 5 | - | ||
| 6 | + | The repeat .. until construct is termed a _post_ -test loop, because the controlling condition is tested _after_ each iteration of the loop. | |
| 6 | 7 | ||
| 7 | - | ||
| 8 | + | It has the following syntax: | |
| 9 | + | ``` | |
| 8 | 10 | repeat | |
| 9 | - | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 11 | + | statement1; | |
| 12 | + | // statement2; | |
| 13 | + | // further statements... | |
| 14 | + | until BooleanExpression; | |
| 13 | 15 | ``` | |
| 14 | 16 | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 17 | + | 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`. | |
| 18 | 18 | ||
| 19 | - | ||
| 19 | + | For instance, the following `repeat` loop executes at least once: | |
| 20 | + | ``` | |
| 20 | 21 | repeat | |
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 22 | + | WriteLn(Node.Text); | |
| 23 | + | Node := GetNextNode; | |
| 24 | + | until not Assigned(Node); | |
| 24 | 25 | ``` | |
| 25 | 26 | ||
| 26 | - | ||
| 27 | - | ||
| 28 | - | ||
| 27 | + | It assumes that Node is not Nil at the outset. If this assumption is incorrect, the code will fail, and the program may crash. | |
| 29 | 28 | ||
| 30 | - | ||
| 29 | + | A `while` loop is more defensive, since the needed check is performed before any loop statements are executed: | |
| 30 | + | ``` | |
| 31 | 31 | while Assigned(Node) do | |
| 32 | - | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 32 | + | begin | |
| 33 | + | WriteLn(Node.Text); | |
| 34 | + | Node := GetNextNode; | |
| 35 | + | end; | |
| 36 | 36 | ``` | |
| 37 | 37 | ||
| 38 | - | ||
| 39 | - | ||
| 40 | - | ||
| 38 | + | Use a `repeat` loop when the looping statement(s) must execute at least once, whatever the initial value of the controlling Boolean condition. | |
| 41 | 39 | ||
| 42 | - | ||
| 40 | + | 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. | |
| 43 | 41 | ||
| 44 | - | ||
| 42 | + | 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: | |
| 43 | + | ``` | |
| 45 | 44 | repeat | |
| 46 | - | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 45 | + | statement1; | |
| 46 | + | if Condition then | |
| 47 | + | Break; | |
| 48 | + | statement_that_might_affect_the_Condition; | |
| 49 | + | until False; | |
| 51 | 50 | ``` | |
| 52 | 51 | ||
| 53 | - | ||
| 52 | + | 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. | |
| 54 | 53 | ||
| 54 | + | [[Basic Pascal Tutorial/Chapter 3/WHILE..DO|◄]] | [[Basic Pascal Tutorial/Contents|▲]] | [[Basic Pascal Tutorial/Chapter 3/FOR..IN|►]] | |
| 55 | + | ---|---|--- | |
| 56 | + | ||
| 55 | 57 | --- | |
| 56 | - | ||
| 58 | + | *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](/wiki-contact).* |
