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