Difference between revisions of "Break"
m (→see also: fix wiki syntax error) |
m (syntax highlight line containing “break”) |
||
Line 11: | Line 11: | ||
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 abort prior reaching the data type's boundaries, and b) while still using the advantage of the <syntaxhighlight lang="pascal" enclose="none">for</syntaxhighlight>-construct, that is condition-checking and automatically [[Inc|incrementing]] a variable. | 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 abort prior reaching the data type's boundaries, and b) while still using the advantage of the <syntaxhighlight lang="pascal" enclose="none">for</syntaxhighlight>-construct, that is condition-checking and automatically [[Inc|incrementing]] a variable. | ||
− | <syntaxhighlight lang="pascal" line start="0"> | + | <syntaxhighlight lang="pascal" line start="0" highlight="14"> |
program collatz(input, output, stderr); | program collatz(input, output, stderr); | ||
Revision as of 02:33, 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
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 abort prior reaching the data type's boundaries, and b) while still using the advantage of the for
-construct, that is condition-checking and automatically incrementing a variable.
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 if (n = 1) or (n > (high(n) / 3 - 1)) then
11 begin
12 // leave loop, as next value may get out of range
13 break;
14 end;
15
16 if n mod 2 = 0 then
17 // n is even
18 begin
19 n := n div 2;
20 end
21 // n is odd
22 else
23 begin
24 n := 3 * n + 1;
25 end;
26 end;
27end;
28
29var
30 n: longword;
31begin
32 readLn(n);
33
34 if n < 1 then
35 begin
36 writeLn(stderr, 'not a positive integer');
37 halt(1);
38 end;
39
40 collatzIterative(n);
41end.
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
.
see also
sources
- 1
- compare remarks in the reference manual § “The
For..to
/downto..do
statement” and § “reserved words”