Sign in Join
Wiki copytemporary

Continue: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Continue

OldNewLine
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+
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+
5+## application
6+
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+
9+``` pascal
10+program continueDemo(input, output, stdErr);
11+var
12+ i, sum: integer;
13+begin
14+ sum := 0;
15+ writeLn('Enter some positive numbers, line by line:');
16+
17+ // ┌─────────────────────────────────┐
18+ // ↓ next iteration: check condition │
19+ while not eof do // │
20+ begin // │
21+ readLn(i); // │
22+ // │
23+ if i < 1 then // │
24+ begin // │
25+ writeLn('Hey! Stay positive!'); // │
26+ continue; // → ────────────────────┘
27+ end;
28+
29+ {$push}
30+ {$rangeChecks off}
31+ sum := sum + i;
32+ {$pop}
33+ if sum < 0 then
34+ begin
35+ writeLn('I can’t handle that much positivity.');
36+ halt(1);
37+ end;
38+ end;
39+
40+ writeLn('The sum of all your positive numbers is ', sum, '.');
41+ writeLn('I like that positivity.');
42+end.
43+```
44+
45+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+
47+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+
49+## see also
50+
51+- [`continue`](https://www.freepascal.org/docs-html/rtl/system/continue.html)
52+- [[Break|break]]
53+
54+---
55+*From the Free Pascal wiki, page "Continue", as it stood on 2021-07-15 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).*

Back to history