Repeat: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Repeat
| Old | New | Line | |
|---|---|---|---|
| 1 | + | This [[Reserved word|reserved words]] `repeat` in conjunction with `until` are used to create tail-controlled [[Loops|loops]]. | |
| 2 | + | ||
| 3 | + | ## syntax | |
| 4 | + | ||
| 5 | + | A tail-controlled loops start with `repeat`, followed by a possibly empty list of [[statement|statements]], and concluded by `until` and a `Boolean` [[expression]]. | |
| 6 | + | ||
| 7 | + | The following ([[Infinite loop|infinite]]) loops demonstrate the syntax: | |
| 8 | + | ||
| 9 | + | ``` pascal | |
| 10 | + | // empty loop body is legal: | |
| 11 | + | repeat | |
| 12 | + | until false; | |
| 13 | + | ``` | |
| 14 | + | ||
| 15 | + | Note, `repeat` and `until` already form a frame in their own right. You do not need to surround your statements by an extra [[Begin|begin]] … [[End|end]]-frame. In fact, you can put as many sequences (also called *compound statements*) between `repeat` and `until` as you want: | |
| 16 | + | ||
| 17 | + | ``` pascal | |
| 18 | + | repeat | |
| 19 | + | begin | |
| 20 | + | write('x'); | |
| 21 | + | end; | |
| 22 | + | begin | |
| 23 | + | write('o'); | |
| 24 | + | end; | |
| 25 | + | until false; | |
| 26 | + | ``` | |
| 27 | + | ||
| 28 | + | Note, it is not necessary, but allowed to put a [[;|semicolon]] prior `until`: | |
| 29 | + | ||
| 30 | + | ``` pascal | |
| 31 | + | repeat | |
| 32 | + | write('zZ') | |
| 33 | + | until false; | |
| 34 | + | ``` | |
| 35 | + | ||
| 36 | + | ## semantics | |
| 37 | + | ||
| 38 | + | 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|false]], another iteration occurs. | |
| 39 | + | ||
| 40 | + | Therefore, `repeat` … `until` loops are particularly useful to ensure a certain sequence of statements is run *at least* once. | |
| 41 | + | ||
| 42 | + | ``` pascal | |
| 43 | + | repeat | |
| 44 | + | write('Enter a positive number: '); | |
| 45 | + | readLn(i); | |
| 46 | + | ||
| 47 | + | // readLn loads a default value if the source is EOF. | |
| 48 | + | // For integer values the default is zero. | |
| 49 | + | // Since our loop condition requires _positive_ values, | |
| 50 | + | // this loop would be stuck _indefinitely_ if EOF(input). | |
| 51 | + | // Ergo, we check for that: | |
| 52 | + | if eof(input) then | |
| 53 | + | begin | |
| 54 | + | writeLn; | |
| 55 | + | writeLn(stdErr, 'error: input has reached EOF'); | |
| 56 | + | halt(1); | |
| 57 | + | end; | |
| 58 | + | until i > 0; | |
| 59 | + | ``` | |
| 60 | + | ||
| 61 | + | The user will be prompted again and again, but *at least once*, until he finally enters a positive number. | |
| 62 | + | ||
| 63 | + | Another standard usage example is the *reverse* Horner scheme as demonstrated in [[Base converting]]. | |
| 64 | + | ||
| 65 | + | ## see also | |
| 66 | + | ||
| 67 | + | - [[While|while]] for *head*-controlled loops | |
| 68 | + | - [[Break|break]] | |
| 69 | + | - [[Continue|continue]] | |
| 70 | + | - Object Pascal Tutorial on [[Basic Pascal Tutorial/Chapter 3/REPEAT..UNTIL|repeat…until]] | |
| 71 | + | ||
| 72 | + | --- | |
| 73 | + | *From the Free Pascal wiki, page "Repeat", as it stood on 2023-01-25 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).* | |
| 74 | + | Category: Code |
