Basic Pascal Tutorial/Chapter 3/WHILE..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/WHILE..DO
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## While ... DO loops | |
| 2 | + | ||
| 3 | + | 3Db - WHILE..DO (author: Tao Yue, state: unchanged) | |
| 4 | + | ||
| 5 | + | The pretest loop has the following format: | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | while BooleanExpression do | |
| 9 | + | statement; | |
| 10 | + | ``` | |
| 11 | + | ||
| 12 | + | The loop continues to execute until the Boolean expression becomes `FALSE`. In the body of the loop, you must somehow affect the Boolean expression by changing one of the variables used in it. Otherwise, an infinite loop will result: | |
| 13 | + | ||
| 14 | + | ``` pascal | |
| 15 | + | a := 5; | |
| 16 | + | while a < 6 do | |
| 17 | + | writeln (a); | |
| 18 | + | ``` | |
| 19 | + | ||
| 20 | + | Remedy this situation by changing the variable's value: | |
| 21 | + | ||
| 22 | + | ``` pascal | |
| 23 | + | a := 5; | |
| 24 | + | while a < 6 do | |
| 25 | + | begin | |
| 26 | + | writeln (a); | |
| 27 | + | a := a + 1 | |
| 28 | + | end; | |
| 29 | + | ``` | |
| 30 | + | ||
| 31 | + | The `WHILE ... DO` loop is called a pretest loop because the condition is tested before the body of the loop executes. So if the condition starts out as `FALSE`, the body of the `while` loop never executes. | |
| 32 | + | ||
| 33 | + | ## See also | |
| 34 | + | ||
| 35 | + | - [[Basic Pascal Tutorial/Chapter 3/FOR..DO|FOR ...DO loops]] | |
| 36 | + | - [[Until|Repeat... Until loops]] | |
| 37 | + | - [[for-in loop|For... in loops]] | |
| 38 | + | ||
| 39 | + | --- | |
| 40 | + | *From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/WHILE..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).* |
