Difference between revisions of "Break"
(more justification) |
m (ordinal numbers in syntaxhighlight) |
||
Line 12: | Line 12: | ||
<!-- 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 --> | ||
− | <syntaxhighlight lang="pascal" line | + | <syntaxhighlight lang="pascal" line highlight="15"> |
program collatz(input, output, stderr); | program collatz(input, output, stderr); | ||
Revision as of 18:01, 7 March 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).
1program collatz(input, output, stderr);
2
3procedure collatzIterative(n: qword);
4var
5 i: qword;
6begin
7 for i := 0 to high(i) do
8 begin
9 writeLn('step ', i:20, ': ', n);
10
11 // Collatz conjecture: sequence ends with 1
12 if (n = 1) or (n > (high(n) / 3 - 1)) then
13 begin
14 // leave loop, as next value may get out of range
15 break;
16 end;
17
18 // n := ifThen(n mod 2 = 0, n div 2, 3 * n + 1);
19 if n mod 2 = 0 then
20 // n is even
21 begin
22 n := n div 2;
23 end
24 // n is odd
25 else
26 begin
27 n := 3 * n + 1;
28 end;
29 end;
30end;
31
32var
33 n: longword;
34begin
35 readLn(n);
36
37 if n < 1 then
38 begin
39 writeLn(stderr, 'not a positive integer');
40 halt(1);
41 end;
42
43 collatzIterative(n);
44end.
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”