Difference between revisions of "Break"
m (highlighting shift) |
(more justification) |
||
Line 4: | Line 4: | ||
Its primary application is to exit a loop prior its planned end. | Its primary application is to exit a loop prior its planned end. | ||
− | <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight> can only be written within loops. | + | <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight>, 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 identfier {{Doc|package=RTL|unit=system|identifier=break|text=<syntaxhighlight lang="pascal" enclose="none">system.break</syntaxhighlight>}} at any time, though. | It is not a reserved word¹, therefore you could shadow it, but access it by writing the fully qualified identfier {{Doc|package=RTL|unit=system|identifier=break|text=<syntaxhighlight lang="pascal" enclose="none">system.break</syntaxhighlight>}} at any time, though. | ||
Example: | Example: | ||
The following program tackles the [https://en.wikipedia.org/wiki/Collatz_conjecture Collatz problem]. | The following program tackles the [https://en.wikipedia.org/wiki/Collatz_conjecture Collatz problem]. | ||
− | The [[For|<syntaxhighlight lang="pascal" enclose="none">for</syntaxhighlight>-loop]] in <syntaxhighlight lang="pascal" enclose="none">collatzIterative</syntaxhighlight> uses a <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight>, 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 <syntaxhighlight lang="pascal" enclose="none">for</syntaxhighlight>-construct | + | The [[For|<syntaxhighlight lang="pascal" enclose="none">for</syntaxhighlight>-loop]] in <syntaxhighlight lang="pascal" enclose="none">collatzIterative</syntaxhighlight> uses a <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight>, 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 <syntaxhighlight lang="pascal" enclose="none">for</syntaxhighlight>-construct (i.e. automatically [[Inc|incrementing]] a variable within a specified range). |
<!-- leave the ifThen expanded, for those who aren't quite familiar with the math unit --> | <!-- leave the ifThen expanded, for those who aren't quite familiar with the math unit --> | ||
Line 58: | Line 58: | ||
end. | end. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | Choosing a <syntaxhighlight lang="pascal" enclose="none">for</syntaxhighlight>-loop in conjunction with a <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight> is adequate, since the Collatz conjecture ''hypothesizes'' that the described function eventually ends in <syntaxhighlight lang="pascal" enclose="none">1</syntaxhighlight>, but does not tell for sure. | ||
+ | Therefore – mathematically speaking – writing <syntaxhighlight lang="pascal" enclose="none">while n <> 1 do</syntaxhighlight> does not consider the circumstance, that the problem is an ''assumption'', but would suggest it is determined to eventually result in <syntaxhighlight lang="pascal" enclose="none">n = 1</syntaxhighlight>. | ||
− | + | However, the usage of <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight> is usually considered as bad style, since it “delegitimizes” the loop's condition expression. | |
You have to ''know'' a loop's statement block contains a <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight> to determine all abort conditions. | You have to ''know'' a loop's statement block contains a <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight> to determine all abort conditions. | ||
− | According to the [[GNU Pascal|GP]]C manual, <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight> is a Borland Pascal extension, whereas Mac Pascal has <syntaxhighlight lang="pascal" enclose="none">leave</syntaxhighlight>. | + | According to the [[GNU Pascal|GP]]C manual, <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight> is a [[Borland Pascal]] extension, whereas [[Mac Pascal]] has <syntaxhighlight lang="pascal" enclose="none">leave</syntaxhighlight>. |
+ | [[FPC]] only knows <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight>, though. | ||
== see also == | == see also == | ||
Line 68: | Line 71: | ||
* [[Exit|<syntaxhighlight lang="pascal" enclose="none">exit</syntaxhighlight>]] to return from routines | * [[Exit|<syntaxhighlight lang="pascal" enclose="none">exit</syntaxhighlight>]] to return from routines | ||
* [[Continue|<syntaxhighlight lang="pascal" enclose="none">continue</syntaxhighlight>]] to skip the rest of an iteration | * [[Continue|<syntaxhighlight lang="pascal" enclose="none">continue</syntaxhighlight>]] to skip the rest of an iteration | ||
+ | * [[Goto|<syntaxhighlight lang="pascal" enclose="none">goto</syntaxhighlight>]]<!-- an equally disfavored statement as “break” --> | ||
<small> | <small> |
Revision as of 18:42, 14 February 2018
│
Deutsch (de) │
English (en) │
español (es) │
suomi (fi) │
français (fr) │
русский (ru) │
The break
routine effectively destroys a loop.
Its primary application is to exit a loop prior its planned end.
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 identfier system.break
at any time, though.
Example:
The following program tackles the Collatz problem.
The 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 incrementing a variable within a specified range).
0program collatz(input, output, stderr);
1
2procedure collatzIterative(n: qword);
3var
4 i: qword;
5begin
6 for i := 0 to high(i) do
7 begin
8 writeLn('step ', i:20, ': ', n);
9
10 // Collatz conjecture: sequence ends with 1
11 if (n = 1) or (n > (high(n) / 3 - 1)) then
12 begin
13 // leave loop, as next value may get out of range
14 break;
15 end;
16
17 // n := ifThen(n mod 2 = 0, n div 2, 3 * n + 1);
18 if n mod 2 = 0 then
19 // n is even
20 begin
21 n := n div 2;
22 end
23 // n is odd
24 else
25 begin
26 n := 3 * n + 1;
27 end;
28 end;
29end;
30
31var
32 n: longword;
33begin
34 readLn(n);
35
36 if n < 1 then
37 begin
38 writeLn(stderr, 'not a positive integer');
39 halt(1);
40 end;
41
42 collatzIterative(n);
43end.
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 is determined to eventually result in n = 1
.
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 statement block contains a break
to determine all abort conditions.
According to the GPC manual, break
is a Borland Pascal extension, whereas Mac Pascal has leave
.
FPC only knows break
, though.
see also
break
in thesystem
unitexit
to return from routinescontinue
to skip the rest of an iterationgoto
sources
- 1
- compare remarks in the reference manual § “The
For..to
/downto..do
statement” and § “reserved words”