Loops: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Loops
| Old | New | Line | |
|---|---|---|---|
| 1 | + | A **loop** control structure repeats a [[statement]] as long as a certain condition is met. | |
| 2 | + | ||
| 3 | + | ## properties | |
| 4 | + | ||
| 5 | + | A loop is sectioned into a | |
| 6 | + | ||
| 7 | + | - loop head, and a | |
| 8 | + | - loop body. | |
| 9 | + | ||
| 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 | + | ||
| 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 | + | ||
| 14 | + | ## types | |
| 15 | + | ||
| 16 | + | [[Standard Pascal|Pascal]] defines | |
| 17 | + | ||
| 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 | + | ||
| 23 | + | The [[FPC]] also supports [[for-in loop|for … in … do]] loops, which are similar to counting loops. | |
| 24 | + | ||
| 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 | + | ||
| 27 | + | ## comparative remarks | |
| 28 | + | ||
| 29 | + | 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 | + | ||
| 31 | + | ## see also | |
| 32 | + | ||
| 33 | + | - [[Pascal basics]] | |
| 34 | + | - [[Main Loop Hooks]] | |
| 35 | + | ||
| 36 | + | --- | |
| 37 | + | *From the Free Pascal wiki, page "Loops", as it stood on 2021-01-26 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).* | |
| 38 | + | Category: Control Structures | |
| 39 | + | Category: Pascal |
