Difference between revisions of "Break"

From Lazarus wiki
Jump to navigationJump to search
(add more explanation)
(more links, mood, wording, unify source code style, typography, spelling mistake)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{Break}}
 
{{Break}}
  
The <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight> routine effectively destroys a [[Loop|loop]].
+
The <syntaxhighlight lang="pascal" inline>break</syntaxhighlight> (pseudo) [[Routine|routine]] effectively destroys a [[Loops|loop]].
 
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" inline>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 [[Identifier|identifier]] {{Doc|package=RTL|unit=system|identifier=break|text=<syntaxhighlight lang="pascal" inline>system.break</syntaxhighlight>}} at any time, though.
  
 +
== Collatz conjecture ==
 
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, that is condition-checking and automatically [[Inc|incrementing]] a variable.
+
The [[For|<syntaxhighlight lang="pascal" inline>for</syntaxhighlight>-loop]] in <syntaxhighlight lang="pascal" inline>collatzIterative</syntaxhighlight> uses a <syntaxhighlight lang="pascal" inline>break</syntaxhighlight>, a)&nbsp;to check for the terminating condition according to Collatz’ problem, b)&nbsp;to abort prior reaching the data type’s boundaries, and c)&nbsp;while still using the advantage of the <syntaxhighlight lang="pascal" inline>for</syntaxhighlight>-construct (i.&#8239;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 -->
<syntaxhighlight lang="pascal" line start="0" highlight="14">
+
<syntaxhighlight lang="pascal" line highlight="15">
 
program collatz(input, output, stderr);
 
program collatz(input, output, stderr);
  
Line 30: Line 31:
 
end;
 
end;
 
 
// n := ifThen(n mod 2 = 0, n div 2, 3 * n + 1);
+
// n := ifThen(odd(n), 3 * n + 1, n div 2);
if n mod 2 = 0 then
+
if odd(n) then
// n is even
+
// n is odd
 
begin
 
begin
n := n div 2;
+
n := 3 * n + 1;
 
end
 
end
// n is odd
+
// n is even
 
else
 
else
 
begin
 
begin
n := 3 * n + 1;
+
n := n div 2;
 
end;
 
end;
 
end;
 
end;
Line 58: Line 59:
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
Choosing a <syntaxhighlight lang="pascal" inline>for</syntaxhighlight>-loop in conjunction with a <syntaxhighlight lang="pascal" inline>break</syntaxhighlight> is adequate, since the Collatz conjecture ''hypothesizes'' that the described function eventually ends in <syntaxhighlight lang="pascal" inline>1</syntaxhighlight>, but does not tell for sure.
 +
Therefore – mathematically speaking – writing <syntaxhighlight lang="pascal" inline>while n <> 1 do</syntaxhighlight> does not consider the circumstance, that the problem is an ''assumption'', but would suggest it was ''determined'' to eventually result in <syntaxhighlight lang="pascal" inline>n = 1</syntaxhighlight>.
  
The usage of <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight> is usually considered as bad style, since it “delegitimizes” the loop's condition expression.
+
== Other remarks ==
You have to ''know'' a loop's statement block contains a <syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight> to determine all abort conditions.
+
However, the usage of <syntaxhighlight lang="pascal" inline>break</syntaxhighlight> is usually considered as bad style, since it “delegitimizes” the loop’s condition expression.
 +
You have to ''know'' a loop’s body contains a <syntaxhighlight lang="pascal" inline>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" inline>break</syntaxhighlight> is a [[Borland Pascal]] extension, whereas [[Mac Pascal]] has <syntaxhighlight lang="pascal" inline>leave</syntaxhighlight>.
 +
[[FPC]], apart from [[Mode MacPas|<syntaxhighlight lang="pascal" inline>{$mode MacPas}</syntaxhighlight>]], only knows <syntaxhighlight lang="pascal" inline>break</syntaxhighlight>, though.
  
== see also ==
+
== See also ==
* {{Doc|package=RTL|unit=system|identifier=break|text=<syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight>}} in the <syntaxhighlight lang="pascal" enclose="none">system</syntaxhighlight> unit
+
* {{Doc|package=RTL|unit=system|identifier=break|text=<syntaxhighlight lang="pascal" inline>break</syntaxhighlight>}} in the <syntaxhighlight lang="pascal" inline>system</syntaxhighlight> unit
* [[Exit|<syntaxhighlight lang="pascal" enclose="none">exit</syntaxhighlight>]] to return from routines
+
* [[Exit|<syntaxhighlight lang="pascal" inline>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" inline>continue</syntaxhighlight>]] to skip the rest of an iteration
 +
* [[Goto|<syntaxhighlight lang="pascal" inline>goto</syntaxhighlight>]]<!-- an equally disfavored statement as “break” -->
  
 
<small>
 
<small>
 
 
== sources ==
 
== sources ==
 
; 1
 
; 1
: compare remarks in [https://www.freepascal.org/docs-html/ref/refsu58.html the reference manual § “The <syntaxhighlight lang="pascal" enclose="none">For..to</syntaxhighlight>/<syntaxhighlight lang="pascal" enclose="none">downto..do</syntaxhighlight> statement”] and [https://www.freepascal.org/docs-html/ref/refsu3.html § “reserved words”]
+
: compare remarks in [https://www.freepascal.org/docs-html/ref/refsu58.html the reference manual § “The <syntaxhighlight lang="pascal" inline>For..to</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>downto..do</syntaxhighlight> statement”] and [https://www.freepascal.org/docs-html/ref/refsu3.html § “reserved words”]
 
</small>
 
</small>
  
 
[[Category:Code]]
 
[[Category:Code]]

Latest revision as of 17:12, 20 November 2020

Deutsch (de) English (en) español (es) suomi (fi) français (fr) русский (ru)

The break (pseudo) 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 identifier system.break at any time, though.

Collatz conjecture

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(odd(n), 3 * n + 1, n div 2);
19		if odd(n) then
20		// n is odd
21		begin
22			n := 3 * n + 1;
23		end
24		// n is even
25		else
26		begin
27			n := n div 2;
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 was determined to eventually result in n = 1.

Other remarks

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.

According to the GPC manual, break is a Borland Pascal extension, whereas Mac Pascal has leave. FPC, apart from {$mode MacPas}, only knows break, though.

See also

sources

1
compare remarks in the reference manual § “The For..to/downto..do statement” and § “reserved words”