Break: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Break
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The `break` (pseudo) [[Routine|routine]] effectively destroys a [[Loops|loop]]. Its primary application is to exit a loop prior its planned end. | |
| 2 | + | ||
| 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 | + | ||
| 5 | + | ## Collatz conjecture | |
| 6 | + | ||
| 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 | + | ||
| 9 | + | ``` pascal | |
| 10 | + | program collatz(input, output, stderr); | |
| 11 | + | ||
| 12 | + | procedure collatzIterative(n: qword); | |
| 13 | + | var | |
| 14 | + | i: qword; | |
| 15 | + | begin | |
| 16 | + | for i := 0 to high(i) do | |
| 17 | + | begin | |
| 18 | + | writeLn('step ', i:20, ': ', n); | |
| 19 | + | ||
| 20 | + | // Collatz conjecture: sequence ends with 1 | |
| 21 | + | if (n = 1) or (n > (high(n) / 3 - 1)) then | |
| 22 | + | begin | |
| 23 | + | // leave loop, as next value may get out of range | |
| 24 | + | break; | |
| 25 | + | end; | |
| 26 | + | ||
| 27 | + | // n := ifThen(odd(n), 3 * n + 1, n div 2); | |
| 28 | + | if odd(n) then | |
| 29 | + | // n is odd | |
| 30 | + | begin | |
| 31 | + | n := 3 * n + 1; | |
| 32 | + | end | |
| 33 | + | // n is even | |
| 34 | + | else | |
| 35 | + | begin | |
| 36 | + | n := n div 2; | |
| 37 | + | end; | |
| 38 | + | end; | |
| 39 | + | end; | |
| 40 | + | ||
| 41 | + | var | |
| 42 | + | n: longword; | |
| 43 | + | begin | |
| 44 | + | readLn(n); | |
| 45 | + | ||
| 46 | + | if n < 1 then | |
| 47 | + | begin | |
| 48 | + | writeLn(stderr, 'not a positive integer'); | |
| 49 | + | halt(1); | |
| 50 | + | end; | |
| 51 | + | ||
| 52 | + | collatzIterative(n); | |
| 53 | + | end. | |
| 54 | + | ``` | |
| 55 | + | ||
| 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 | + | ||
| 58 | + | ## Other remarks | |
| 59 | + | ||
| 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 | + | ||
| 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 | + | ||
| 64 | + | ## See also | |
| 65 | + | ||
| 66 | + | - [`break`](https://www.freepascal.org/docs-html/rtl/system/break.html) in the `system` unit | |
| 67 | + | - [[Exit|exit]] to return from *routines* | |
| 68 | + | - [[Continue|continue]] to skip the rest of an iteration | |
| 69 | + | - [[Goto|goto]] | |
| 70 | + | ||
| 71 | + | <small> | |
| 72 | + | ||
| 73 | + | ## sources | |
| 74 | + | ||
| 75 | + | 1 | |
| 76 | + | 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 | + | </small> | |
| 79 | + | ||
| 80 | + | --- | |
| 81 | + | *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 | + | Category: Code |
