Sign in Join
Wiki copytemporary

For: what changed

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

Title: For

OldNewLine
1+`for` is a [[Keyword|keyword]] used in conjunction with other keywords to create [[Loops|loops]].
2+
3+## loop with iterator variable
4+
5+`for` used along with `to`/`downto` and [[Do|do]] constructs a loop in which the value of a control variable is incremented or decremented by `1` passing every iteration.
6+
7+### behavior
8+
9+``` pascal
10+for controlVariable := start to finalValue do
11+begin
12+ statement;
13+end;
14+```
15+
16+In this example `controlVariable` is first initialized with the value of `start` (but cmp. § “legacy” below). If and as long as `controlVariable` is not greater than `finalValue`, the [[Begin|begin]] … [[End|end]] [[Frame|frame]] with all its [[statement|statements]] is executed. By reaching `end` `controlVariable` is [[Inc|incremented]] by `1` and the comparison is made, whether another iteration, whether the statement-frame is executed again.
17+
18+### reverse direction
19+
20+By exchanging `to` with `downto`, the variable – in the example `controlVariable` – is [[Dec|decremented]] by `1` and the condition becomes “as long as the control variable is not less than the final value.”
21+
22+### constraints
23+
24+#### scope requirements
25+
26+The control variable has to be [[Local variables|local]] inside nested [[Routine|routines]]. A routine is nested, if [[Procedural variable|routine variables]] tagged with the modifier `is nested` can store its address. Nevertheless, a [[Global variables|global variable]] is always allowed as a control variable.
27+
28+#### immutable requirement
29+
30+While inside in a loop, it is imperative not to mess with the loop variable. Plain assignments – e. g. `controlVariable := 2` – are caught by the compiler reporting “Illegal assignment to for-loop variable "controlVariable"”. However, *indirect* manipulations are not prevented:
31+
32+``` pascal
33+program iteratorManipulation(input, output, stderr);
34+
35+var
36+ i: longint;
37+
38+procedure foo;
39+begin
40+ i := 1;
41+end;
42+
43+begin
44+ for i := 0 to 2 do
45+ begin
46+ writeLn(i);
47+ foo;
48+ end;
49+end.
50+```
51+
52+The global variable `i` is modified by calling `foo`. The program runs an [[Infinite loop|infinite loop]].
53+
54+#### type restrictions
55+
56+The [[Data type|type]] of `controlVariable` has to be enumerable. Assuming your system operates on [[ASCII]] you can do
57+
58+``` pascal
59+var
60+ c: char;
61+begin
62+ for c := 'a' to 'z' do
63+ begin
64+ writeLn(c);
65+ end;
66+end.
67+```
68+
69+or generally use any type that is enumerable.
70+
71+#### other step widths
72+
73+Other step widths than `1` or `-1` are not possible by utilizing this syntax. You have to use other loop constructs such as [[While|while]] … `do` and manually initialize, compare and change the `controlVariable`.
74+
75+The matter, whether [[FPC|FreePascal]] could provide a syntax specifying other step widths, came up several times. In general it was regarded as “syntactic sugar”, though, and in order to stay compatible Pascal’s core, or Delphi’s extensions, in order to avoid any impetuous decisions, the developers remained rather conservative and rejected any changes in that direction.
76+
77+<sup>See also [FPC issue 25549](https://gitlab.com/freepascal.org/fpc/source/-/issues/25549).</sup>
78+
79+### limits
80+
81+Note, as it is common in mathematics when writing sums $`\sum_{n=0}^{k}`$ or products $`\prod_{n=1}^{k}`$ the limits are *inclusive*.
82+
83+``` pascal
84+for i := 5 to 5 do
85+begin
86+ writeLn(i);
87+end;
88+```
89+
90+This excerpt will print one line with `5`, though you might be fooled by such thoughts as “5 to 5 – the difference is zero. So the body must never be executed.” This is in fact wrong. Not the *difference* determines the number of iterations, but the cardinality of the set constructed by the expression `[start..finalValue]`. This can be an empty set, or in the case of `[5..5]` just the single-element set $`\left\{5\right\}`$.
91+
92+### legacy
93+
94+Assuming no other manipulations were made, after the loop the value of `controlVariable` will be `final`, unless the the proper condition was not met from the start, then it is *undefined* (remains unchanged).
95+
96+In case of empty loops (where the body is never executed), even the `start` value is not loaded. Rationale: The `controlVariable` exists for usage *inside* the loop’s body. If the loop’s body is not entered, then the value might remain unused. We generally avoid unused values, i. e. any unnecessary assignment without successive reads.
97+
98+### short syntax
99+
100+For *single* statements writing a surrounding `begin` … `end` frame can be skipped resulting in:
101+
102+``` pascal
103+for controlVariable := start to final do
104+ statement;
105+```
106+
107+It is advised though, to make use of that only in justified cases, where the readability is improved and it is very unlikely the loop is expanded by any additional statement. Too many programmers inadvertently fell for the pitfall before and added a properly indented line forgetting it requires a surrounding `begin` … `end` then, too.
108+
109+Make it a habit and always accompany `for`-loops with `begin` … `end`, leaving the option to possibly eliminate those at a *later* stage, prior a complete code freeze (but *not* right in the middle of development).
110+
111+## loop with elements
112+
113+With [[for-in loop|for … in loops]] the variable that is changed every iteration represents an element out of a collection. This works on strings, [[Array|arrays]], [[Set|sets]], and any other custom collection that implements the required iterators. Looping over an empty collection simply does nothing.
114+
115+``` pascal
116+type
117+ furniture = (chair, desk, bed, wardrobe);
118+ arrangement = set of furniture;
119+var
120+ thing: furniture;
121+begin
122+ writeLn('all available pieces of furniture:');
123+ for thing in arrangement do
124+ begin
125+ writeLn(thing);
126+ end;
127+end.
128+```
129+
130+In contrast to other loops, an index variable is not provided. In the above example `ord(thing)` will return an index, but it has to be *additionally* retrieved while it inherently exists yet still inaccessible. [[for-in loop|Proposals]] were made, whether and how to extend the syntax allowing to specify an index variable that is adjusted with every iteration.
131+
132+## special commands
133+
134+Inside loops (including `for`-loops) two special commands tamper with the regular loop-program-flow. [`system.continue`](https://www.freepascal.org/docs-html/rtl/system/continue.html) skips the rest of the statements in *one* iteration. Effectively in a `for`-loop the next element/index is immediately assigned to the control variable, the regular check is performed, and processing of all the statements might start from the top again. Additionally [[Break|system.break]] instantly skips the whole loop altogether. This is different to [`system.exit`](https://www.freepascal.org/docs-html/rtl/system/exit.html), which exits the whole frame, but loops do not create frames (but [[Block|blocks]] do).
135+
136+Their usage however is usually discredited, since they “disqualify” the loop’s iteration condition. One has to *know* a loop’s body contains such commands, in order to actually determine how often a loop is executed. Without such, you can tell how many times a loop is run just by inspecting the loop’s head or tail respectively.
137+
138+## see also
139+
140+- [[Example: Why the loop variable should be of signed type]]
141+- [[sRangechecks|{$rangechecks}]]
142+
143+---
144+*From the Free Pascal wiki, page "For", as it stood on 2022-07-16 after a change by Kirinn, 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).*

Back to history