Sign in Join
Wiki copytemporary

Loops: what changed

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

OldNewLine
1-A **loop** control structure repeats a [[statement]] as long as a certain condition is met.
1+A **loop** control structure repeats a [[statement]] as long as a certain condition is met.
22
33## properties
44
5-A loop is sectioned into a
5+A loop is sectioned into a
66
7-- loop head, and a
8-- loop body.
7+ * loop head, and a
8+ * loop body.
99
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.
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.
1111
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.
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.
1313
1414## types
1515
16-[[Standard Pascal|Pascal]] defines
16+[[Standard Pascal|Pascal]] defines
1717
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.
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.
2222
23-The [[FPC]] also supports [[for-in loop|for … in … do]] loops, which are similar to counting loops.
23+The [[FPC]] also supports [[for-in loop|`for … in … do`]] loops, which are similar to counting loops.
2424
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.
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.
2626
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+
2729## comparative remarks
2830
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]].
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]].
3032
3133## see also
3234
33-- [[Pascal basics]]
34-- [[Main Loop Hooks]]
35+ * [[Pascal basics]]
36+ * [[Main Loop Hooks]]
3537
3638---
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
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).*

Back to history