Sign in Join
Wiki copytemporary

Break: what changed

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

OldNewLine
11The `break` (pseudo) [[Routine|routine]] effectively destroys a [[Loops|loop]]. Its primary application is to exit a loop prior its planned end.
22
33`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.
44
55## Collatz conjecture
66
77Example: 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).
88
99``` pascal
1010program collatz(input, output, stderr);
1111
1212procedure collatzIterative(n: qword);
1313var
1414 i: qword;
1515begin
1616 for i := 0 to high(i) do
1717 begin
1818 writeLn('step ', i:20, ': ', n);
1919
2020 // Collatz conjecture: sequence ends with 1
2121 if (n = 1) or (n > (high(n) / 3 - 1)) then
2222 begin
2323 // leave loop, as next value may get out of range
2424 break;
2525 end;
2626
2727 // n := ifThen(odd(n), 3 * n + 1, n div 2);
2828 if odd(n) then
2929 // n is odd
3030 begin
3131 n := 3 * n + 1;
3232 end
3333 // n is even
3434 else
3535 begin
3636 n := n div 2;
3737 end;
3838 end;
3939end;
4040
4141var
4242 n: longword;
4343begin
4444 readLn(n);
4545
4646 if n < 1 then
4747 begin
4848 writeLn(stderr, 'not a positive integer');
4949 halt(1);
5050 end;
5151
5252 collatzIterative(n);
5353end.
5454```
5555
5656Choosing 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`.
5757
5858## Other remarks
5959
6060However, 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.
6161
6262According 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.
6363
6464## See also
6565
6666- [`break`](https://www.freepascal.org/docs-html/rtl/system/break.html) in the `system` unit
6767- [[Exit|exit]] to return from *routines*
6868- [[Continue|continue]] to skip the rest of an iteration
6969- [[Goto|goto]]
7070
71-<small>
72-
7371## sources
7472
75731
7674compare 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>
7975
8076---
8177*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).*
8278Category: Code

Back to history