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 considers 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.
program collatz(input, output, stderr);
procedure collatzIterative(n: qword);
var
i: qword;
begin
for i := 0 to high(i) do
begin
// #9 is a tab character
writeLn(i, #9, n);
if (n = 1) or (n > (high(n) / 3 - 1)) then
begin
// leave loop, as next value may get out of range
break;
end;
if n mod 2 = 0 then
// n is even
begin
n := n div 2;
end
// n is odd
else
begin
n := 3 * n + 1;
end;
end;
end;
var
n: longword;
begin
readLn(n);
if n < 1 then
begin
writeLn(stderr, 'not a positive integer');
halt(1);
end;
collatzIterative(n);
end.
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..dostatement” and § “reserved words”