Sign in Join
Wiki copytemporary

To: what changed

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

OldNewLine
1-Back to [[Reserved words]].
1+Back to [[Reserved words]].
22
3-[[Keyword]] used to indicate the final value of the control value in a [[For]] loop, and that the loop is to increment the control variable by 1 on each loop. The value specified by TO should be greater than the initial value of the for loop.
3+
4+[[Keyword]] used to indicate the final value of the control value in a [[For]] loop, and that the loop is to increment the control variable by 1 on each loop. The value specified by TO should be greater than the initial value of the for loop.
45
56## [[For]] to [[Do|do]]
6-
7-``` pascal
7+```
88var i : integer;
9-begin
10- for i := 1 to 10000 do
11- begin
12- //...
13- end;
14-end;
9+ begin
10+ for i := 1 to 10000 do
11+ begin
12+ //...
13+ end;
14+ end;
1515```
1616
17-The for to allow to execute code repeatedly for a fixed number of times.
17+The for to allow to execute code repeatedly for a fixed number of times.
1818
1919### Basic example
20-
21-``` pascal
20+```
2221var
23- loopValue, startValue, endValue, resultValue: integer;
24-begin
25- startValue := 10;
26- endValue := 11;
27- resultValue := 0;
28- for loopValue := startValue to endValue do
22+ loopValue, startValue, endValue, resultValue: integer;
2923 begin
30- resultValue := loopValue + resultValue;
24+ startValue := 10;
25+ endValue := 11;
26+ resultValue := 0;
27+ for loopValue := startValue to endValue do
28+ begin
29+ resultValue := loopValue + resultValue;
30+ end;
3131 end;
32-end;
3332```
3433
35-The loop execute two times and variable resultValue value is 21.
34+The loop execute two times and variable resultValue value is 21.
3635
3736### Start and end value same
38-
39-``` pascal
37+```
4038var
41- loopValue, startValue, endValue, resultValue: integer;
42-begin
43- startValue := 10;
44- endValue := 10;
45- resultValue := 0;
46- for loopValue := startValue to endValue do
39+ loopValue, startValue, endValue, resultValue: integer;
4740 begin
48- resultValue := loopValue + resultValue;
41+ startValue := 10;
42+ endValue := 10;
43+ resultValue := 0;
44+ for loopValue := startValue to endValue do
45+ begin
46+ resultValue := loopValue + resultValue;
47+ end;
4948 end;
50-end;
5149```
5250
53-The loop execute one time and variable resultValue value is 10.
51+The loop execute one time and variable resultValue value is 10.
5452
5553### Start value bigger than end value
56-
57-``` pascal
54+```
5855var
59- loopValue, startNumber, endNumber, resultValue: integer;
60-begin
61- startValue := 10;
62- endValue := 9;
63- resultValue := 0;
64- for loopValue := startValue to endValue do
56+ loopValue, startNumber, endNumber, resultValue: integer;
6557 begin
66- resultValue := loopValue + resultValue;
58+ startValue := 10;
59+ endValue := 9;
60+ resultValue := 0;
61+ for loopValue := startValue to endValue do
62+ begin
63+ resultValue := loopValue + resultValue;
64+ end;
6765 end;
68-end;
6966```
7067
71-The loop execute none time and variable resultValue value is 0.
68+The loop execute none time and variable resultValue value is 0.
7269
70+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.
71+
7372## Read more
7473
75-- [[Downto]]
74+ * [[Downto]]
7675
7776---
78-*From the Free Pascal wiki, page "To", as it stood on 2020-03-01 after a change by Trev, 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).*
77+*From the Free Pascal wiki, page "To", as the Internet Archive kept it on 2024-09-20, 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