Basic Pascal Tutorial/Chapter 3/FOR..DO: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Basic Pascal Tutorial/Chapter 3/FOR..DO
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## FOR...DO Loops (author: Tao Yue, state: changed) | |
| 2 | + | ||
| 3 | + | FOR...DO is a loop construct in Pascal. Of course, that may raise the question "What is a loop?". | |
| 4 | + | ||
| 5 | + | ### Loops | |
| 6 | + | ||
| 7 | + | Looping means repeating a statement or compound statement over and over until some condition is met. | |
| 8 | + | ||
| 9 | + | There are three types of loops: | |
| 10 | + | ||
| 11 | + | - fixed repetition - only repeats a fixed number of times | |
| 12 | + | - pretest - tests a Boolean expression, then goes into the loop if TRUE | |
| 13 | + | - posttest - executes the loop, then tests the Boolean expression | |
| 14 | + | ||
| 15 | + | ### FOR...DO Loop | |
| 16 | + | ||
| 17 | + | In Pascal, the fixed repetition loop is the for loop. The general form is: | |
| 18 | + | ||
| 19 | + | ``` pascal | |
| 20 | + | for index := StartingLow to EndingHigh do | |
| 21 | + | statement; | |
| 22 | + | ``` | |
| 23 | + | ||
| 24 | + | The index variable must be of an **ordinal** data. The index can be used in calculations within the body of the loop, but its value cannot be changed (i.e. `count:=5` will cause a program exception.) | |
| 25 | + | ||
| 26 | + | In Pascal, the `for` loop can only count in increments (steps) of 1. A loop can be interrupted using the *break* statement. An example of using the index is: | |
| 27 | + | ||
| 28 | + | ``` pascal | |
| 29 | + | sum := 0; | |
| 30 | + | for count := 1 to 100 do | |
| 31 | + | begin | |
| 32 | + | sum := sum + count; | |
| 33 | + | if sum = 38 then break; | |
| 34 | + | end; | |
| 35 | + | ``` | |
| 36 | + | ||
| 37 | + | The computer would do the sum the long way and still finish it in far less time than it took the mathematician Gauss to do the sum the short way (`1+100 = 101. 2+99 = 101`. See a pattern? There are 100 numbers, so the pattern repeats 50 times. `101*50 = 5050`. This isn't advanced mathematics, its attribution to Gauss is probably apocryphal.). | |
| 38 | + | ||
| 39 | + | In the `for-to-do` loop, the starting value MUST be lower than the ending value, or the loop will never execute! If you want to count down, you should use the `for-downto-do` loop: | |
| 40 | + | ||
| 41 | + | ``` pascal | |
| 42 | + | for index := StartingHigh downto EndingLow do | |
| 43 | + | statement; | |
| 44 | + | ``` | |
| 45 | + | ||
| 46 | + | ## See also | |
| 47 | + | ||
| 48 | + | [[Basic Pascal Tutorial/Chapter 3/WHILE..DO|While ...Do loops]] | |
| 49 | + | ||
| 50 | + | [[Until|Repeat... Until loops]] | |
| 51 | + | ||
| 52 | + | [[for-in loop|For... in loops]] | |
| 53 | + | ||
| 54 | + | --- | |
| 55 | + | *From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/FOR..DO", as it stood on 2022-08-20 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).* |
