Loops: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 1 | + | A **loop** control structure repeats a [[statement]] as long as a certain condition is met. | |
| 2 | 2 | ||
| 3 | 3 | ## properties | |
| 4 | 4 | ||
| 5 | - | ||
| 5 | + | A loop is sectioned into a | |
| 6 | 6 | ||
| 7 | - | ||
| 8 | - | ||
| 7 | + | * loop head, and a | |
| 8 | + | * loop body. | |
| 9 | 9 | ||
| 10 | - | ||
| 10 | + | The loop body is a statement, and the head (possibly implicitly) contains a [[Boolean Expressions|Boolean expression]] that determines whether the loop body is executed (again). Every time the statements in the loop body are executed, an _iteration_ occurs. | |
| 11 | 11 | ||
| 12 | - | ||
| 12 | + | Loops are particularly useful as a programming construct, since the loop body is only inserted once in the final program. The so-called “loop unrolling” [[Compiler|compiler]] optimization may _copy_ the loop body multiple times anyway, but still you do not need to literally repeat the statements in your [[Source code|source code]]. Some processors perform particularly well (fast) if a repeating series of instructions occupies a quite _small_ chunk of memory. | |
| 13 | 13 | ||
| 14 | 14 | ## types | |
| 15 | 15 | ||
| 16 | - | ||
| 16 | + | [[Standard Pascal|Pascal]] defines | |
| 17 | 17 | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 18 | + | * counting loops [[For|`for … to|downto do`]] | |
| 19 | + | * conditional loops | |
| 20 | + | * [[While|`while … do`]], and | |
| 21 | + | * [[Repeat|`repeat … until`]] where the loop head appears at the tail. | |
| 22 | 22 | ||
| 23 | - | ||
| 23 | + | The [[FPC]] also supports [[for-in loop|`for … in … do`]] loops, which are similar to counting loops. | |
| 24 | 24 | ||
| 25 | - | ||
| 25 | + | If the loop body has a predictable number of iterations, the loop can be written with any loop type, but a counting loop is usually the most reasonable choice. Likewise, conditional loops are interchangeable, too, but in any given situation either one is more suitable. | |
| 26 | 26 | ||
| 27 | + | Note that the value of the "for loop variable" is undefined after a loop has completed or if a loop is not executed at all. However, if the loop was terminated prematurely with an exception or a break (or even a goto statement), the loop variable retains the value it had when the loop was exited. | |
| 28 | + | ||
| 27 | 29 | ## comparative remarks | |
| 28 | 30 | ||
| 29 | - | ||
| 31 | + | Unlike in some programming languages, in Pascal a loop itself is a statement; it does not yield a value. Also, a loop body does not create a new [[Scope|scope]]. | |
| 30 | 32 | ||
| 31 | 33 | ## see also | |
| 32 | 34 | ||
| 33 | - | ||
| 34 | - | ||
| 35 | + | * [[Pascal basics]] | |
| 36 | + | * [[Main Loop Hooks]] | |
| 35 | 37 | ||
| 36 | 38 | --- | |
| 37 | - | ||
| 38 | - | ||
| 39 | - | ||
| 39 | + | *From the Free Pascal wiki, page "Loops", as the Internet Archive kept it on 2025-01-14, last changed on 2024-08-15. 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).* |
