Continue: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 1 | + | The `continue` (pseudo) [[Routine|routine]] effectively skips the remainder of the [[Loops|loop]] body for one iteration, thus jumping back (or forward) to the loop head. It is a non-standardized extension. | |
| 2 | 2 | ||
| 3 | - | ||
| 3 | + | `Continue`, with its special meaning of terminating one iteration, can only be written _within_ loops. It is not a reserved word, therefore you _could_ use it as an [[Identifier|identifier]], but still access the special routine by writing its _fully qualified_ identifier `system.continue`. In [[Mode MacPas|`{$mode MacPas}`]] the `cycle` statement is an alias for `continue`. | |
| 4 | 4 | ||
| 5 | 5 | ## application | |
| 6 | 6 | ||
| 7 | - | ||
| 8 | - | ||
| 9 | - | ||
| 7 | + | The use of `continue` is usually disfavored since it “delegitimizes” the loop’s condition expression. If the number of iterations can be predicted _before_ the loop starts, it is even considered bad style to use `continue`. Instead it is highly recommended to improve the algorithm. Nevertheless, in the case of _unpredictable_ data, and thus an _unpredictable_ number of (complete) iterations, `continue` is generally acceptable. | |
| 8 | + | ``` | |
| 10 | 9 | program continueDemo(input, output, stdErr); | |
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 27 | - | ||
| 10 | + | var | |
| 11 | + | i, sum: integer; | |
| 12 | + | begin | |
| 13 | + | sum := 0; | |
| 14 | + | writeLn('Enter some positive numbers, line by line:'); | |
| 28 | 15 | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - | ||
| 32 | - | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 36 | - | ||
| 16 | + | // ┌─────────────────────────────────┐ | |
| 17 | + | // ↓ next iteration: check condition │ | |
| 18 | + | while not eof do // │ | |
| 19 | + | begin // │ | |
| 20 | + | readLn(i); // │ | |
| 21 | + | // │ | |
| 22 | + | if i < 1 then // │ | |
| 23 | + | begin // │ | |
| 24 | + | writeLn('Hey! Stay positive!'); // │ | |
| 25 | + | continue; // → ────────────────────┘ | |
| 26 | + | end; | |
| 27 | + | ||
| 28 | + | {$push} | |
| 29 | + | {$rangeChecks off} | |
| 30 | + | sum := sum + i; | |
| 31 | + | {$pop} | |
| 32 | + | if sum < 0 then | |
| 33 | + | begin | |
| 34 | + | writeLn('I can’t handle that much positivity.'); | |
| 35 | + | halt(1); | |
| 36 | + | end; | |
| 37 | 37 | end; | |
| 38 | - | ||
| 39 | - | ||
| 40 | - | ||
| 41 | - | ||
| 42 | - | ||
| 38 | + | ||
| 39 | + | writeLn('The sum of all your positive numbers is ', sum, '.'); | |
| 40 | + | writeLn('I like that positivity.'); | |
| 41 | + | end. | |
| 43 | 42 | ``` | |
| 44 | 43 | ||
| 45 | - | ||
| 44 | + | After and at `continue` (or `cycle` in `{$mode MacPas}`) the program jumps back, or in the case of [[Repeat|`repeat … until`]] forward, to the loop’s condition. The respective condition must be satisfied again before another iteration occurs. | |
| 46 | 45 | ||
| 47 | - | ||
| 46 | + | The use of `continue` can in practice always be avoided by proper [[If|`if` statements]]. It does not increase the power of Pascal. The above _demonstration_ example would be usually written _without_ a `continue`. However, if there would be _many_ or several _nested_ `if` statements, `continue` is preferred to retain _some_ degree of readability. | |
| 48 | 47 | ||
| 49 | 48 | ## see also | |
| 50 | 49 | ||
| 51 | - | ||
| 52 | - | ||
| 50 | + | * [`continue`](<https://www.freepascal.org/docs-html/rtl/system/continue.html>) | |
| 51 | + | * [[Break|`break`]] | |
| 53 | 52 | ||
| 54 | 53 | --- | |
| 55 | - | ||
| 54 | + | *From the Free Pascal wiki, page "Continue", as the Internet Archive kept it on 2022-08-12. 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).* |
