To: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 1 | + | Back to [[Reserved words]]. | |
| 2 | 2 | ||
| 3 | - | ||
| 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. | |
| 4 | 5 | ||
| 5 | 6 | ## [[For]] to [[Do|do]] | |
| 6 | - | ||
| 7 | - | ||
| 7 | + | ``` | |
| 8 | 8 | var i : integer; | |
| 9 | - | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 14 | - | ||
| 9 | + | begin | |
| 10 | + | for i := 1 to 10000 do | |
| 11 | + | begin | |
| 12 | + | //... | |
| 13 | + | end; | |
| 14 | + | end; | |
| 15 | 15 | ``` | |
| 16 | 16 | ||
| 17 | - | ||
| 17 | + | The for to allow to execute code repeatedly for a fixed number of times. | |
| 18 | 18 | ||
| 19 | 19 | ### Basic example | |
| 20 | - | ||
| 21 | - | ||
| 20 | + | ``` | |
| 22 | 21 | var | |
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 27 | - | ||
| 28 | - | ||
| 22 | + | loopValue, startValue, endValue, resultValue: integer; | |
| 29 | 23 | begin | |
| 30 | - | ||
| 24 | + | startValue := 10; | |
| 25 | + | endValue := 11; | |
| 26 | + | resultValue := 0; | |
| 27 | + | for loopValue := startValue to endValue do | |
| 28 | + | begin | |
| 29 | + | resultValue := loopValue + resultValue; | |
| 30 | + | end; | |
| 31 | 31 | end; | |
| 32 | - | ||
| 33 | 32 | ``` | |
| 34 | 33 | ||
| 35 | - | ||
| 34 | + | The loop execute two times and variable resultValue value is 21. | |
| 36 | 35 | ||
| 37 | 36 | ### Start and end value same | |
| 38 | - | ||
| 39 | - | ||
| 37 | + | ``` | |
| 40 | 38 | var | |
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 45 | - | ||
| 46 | - | ||
| 39 | + | loopValue, startValue, endValue, resultValue: integer; | |
| 47 | 40 | begin | |
| 48 | - | ||
| 41 | + | startValue := 10; | |
| 42 | + | endValue := 10; | |
| 43 | + | resultValue := 0; | |
| 44 | + | for loopValue := startValue to endValue do | |
| 45 | + | begin | |
| 46 | + | resultValue := loopValue + resultValue; | |
| 47 | + | end; | |
| 49 | 48 | end; | |
| 50 | - | ||
| 51 | 49 | ``` | |
| 52 | 50 | ||
| 53 | - | ||
| 51 | + | The loop execute one time and variable resultValue value is 10. | |
| 54 | 52 | ||
| 55 | 53 | ### Start value bigger than end value | |
| 56 | - | ||
| 57 | - | ||
| 54 | + | ``` | |
| 58 | 55 | var | |
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 56 | + | loopValue, startNumber, endNumber, resultValue: integer; | |
| 65 | 57 | begin | |
| 66 | - | ||
| 58 | + | startValue := 10; | |
| 59 | + | endValue := 9; | |
| 60 | + | resultValue := 0; | |
| 61 | + | for loopValue := startValue to endValue do | |
| 62 | + | begin | |
| 63 | + | resultValue := loopValue + resultValue; | |
| 64 | + | end; | |
| 67 | 65 | end; | |
| 68 | - | ||
| 69 | 66 | ``` | |
| 70 | 67 | ||
| 71 | - | ||
| 68 | + | The loop execute none time and variable resultValue value is 0. | |
| 72 | 69 | ||
| 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 | + | ||
| 73 | 72 | ## Read more | |
| 74 | 73 | ||
| 75 | - | ||
| 74 | + | * [[Downto]] | |
| 76 | 75 | ||
| 77 | 76 | --- | |
| 78 | - | ||
| 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).* |
