Break: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | The `break` (pseudo) [[Routine|routine]] effectively destroys a [[Loops|loop]]. Its primary application is to exit a loop prior its planned end. | |
| 2 | 2 | ||
| 3 | 3 | `Break`, with its special meaning of abandoning a loop, can only be written *within* loops. It is not a reserved word¹, therefore you could shadow it, but access it by writing the fully qualified [[Identifier|identifier]] [`system.break`](https://www.freepascal.org/docs-html/rtl/system/break.html) at any time, though. | |
| 4 | 4 | ||
| 5 | 5 | ## Collatz conjecture | |
| 6 | 6 | ||
| 7 | 7 | Example: The following program tackles the [Collatz problem](https://en.wikipedia.org/wiki/Collatz_conjecture). The [[For|for-loop]] in `collatzIterative` uses a `break`, a) to check for the terminating condition according to Collatz’ problem, b) to abort prior reaching the data type’s boundaries, and c) while still using the advantage of the `for`-construct (i. e. automatically [[Inc|incrementing]] a variable within a specified range). | |
| 8 | 8 | ||
| 9 | 9 | ``` pascal | |
| 10 | 10 | program collatz(input, output, stderr); | |
| 11 | 11 | ||
| 12 | 12 | procedure collatzIterative(n: qword); | |
| 13 | 13 | var | |
| 14 | 14 | i: qword; | |
| 15 | 15 | begin | |
| 16 | 16 | for i := 0 to high(i) do | |
| 17 | 17 | begin | |
| 18 | 18 | writeLn('step ', i:20, ': ', n); | |
| 19 | 19 | ||
| 20 | 20 | // Collatz conjecture: sequence ends with 1 | |
| 21 | 21 | if (n = 1) or (n > (high(n) / 3 - 1)) then | |
| 22 | 22 | begin | |
| 23 | 23 | // leave loop, as next value may get out of range | |
| 24 | 24 | break; | |
| 25 | 25 | end; | |
| 26 | 26 | ||
| 27 | 27 | // n := ifThen(odd(n), 3 * n + 1, n div 2); | |
| 28 | 28 | if odd(n) then | |
| 29 | 29 | // n is odd | |
| 30 | 30 | begin | |
| 31 | 31 | n := 3 * n + 1; | |
| 32 | 32 | end | |
| 33 | 33 | // n is even | |
| 34 | 34 | else | |
| 35 | 35 | begin | |
| 36 | 36 | n := n div 2; | |
| 37 | 37 | end; | |
| 38 | 38 | end; | |
| 39 | 39 | end; | |
| 40 | 40 | ||
| 41 | 41 | var | |
| 42 | 42 | n: longword; | |
| 43 | 43 | begin | |
| 44 | 44 | readLn(n); | |
| 45 | 45 | ||
| 46 | 46 | if n < 1 then | |
| 47 | 47 | begin | |
| 48 | 48 | writeLn(stderr, 'not a positive integer'); | |
| 49 | 49 | halt(1); | |
| 50 | 50 | end; | |
| 51 | 51 | ||
| 52 | 52 | collatzIterative(n); | |
| 53 | 53 | end. | |
| 54 | 54 | ``` | |
| 55 | 55 | ||
| 56 | 56 | Choosing a `for`-loop in conjunction with a `break` is adequate, since the Collatz conjecture *hypothesizes* that the described function eventually ends in `1`, but does not tell for sure. Therefore – mathematically speaking – writing `while n <> 1 do` does not consider the circumstance, that the problem is an *assumption*, but would suggest it was *determined* to eventually result in `n = 1`. | |
| 57 | 57 | ||
| 58 | 58 | ## Other remarks | |
| 59 | 59 | ||
| 60 | 60 | However, the usage of `break` is usually considered as bad style, since it “delegitimizes” the loop’s condition expression. You have to *know* a loop’s body contains a `break` to determine all abort conditions. | |
| 61 | 61 | ||
| 62 | 62 | According to the [[GNU Pascal|GPC]] manual, `break` is a [[Borland Pascal]] extension, whereas [[Mac Pascal]] has `leave`. [[FPC]], apart from [[Mode MacPas|{$mode MacPas}]], only knows `break`, though. | |
| 63 | 63 | ||
| 64 | 64 | ## See also | |
| 65 | 65 | ||
| 66 | 66 | - [`break`](https://www.freepascal.org/docs-html/rtl/system/break.html) in the `system` unit | |
| 67 | 67 | - [[Exit|exit]] to return from *routines* | |
| 68 | 68 | - [[Continue|continue]] to skip the rest of an iteration | |
| 69 | 69 | - [[Goto|goto]] | |
| 70 | 70 | ||
| 71 | - | ||
| 72 | - | ||
| 73 | 71 | ## sources | |
| 74 | 72 | ||
| 75 | 73 | 1 | |
| 76 | 74 | compare remarks in [the reference manual § “The `For..to`/`downto..do` statement”](https://www.freepascal.org/docs-html/ref/refsu58.html) and [§ “reserved words”](https://www.freepascal.org/docs-html/ref/refsu3.html) | |
| 77 | - | ||
| 78 | - | ||
| 79 | 75 | ||
| 80 | 76 | --- | |
| 81 | 77 | *From the Free Pascal wiki, page "Break", as it stood on 2020-11-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).* | |
| 82 | 78 | Category: Code |
