Description |
The Break procedure forces a jump out of the setof statements within a loop. Like the Goto statement, it should be used with caution.
The next statement that is executed is the one following the loop terminator. For example:
for i := 1 to 10 do
begin
...
break;
...
end;
size := 10; // breaks to here
It is important to note that the Break statement only jumps out of the current loop - not out of any nested loops above it. The Goto statement can.
|
|
Notes |
Use with caution.
|
|
Related commands |
Continue |
|
Forces a jump to the next iteration of a loop |
Exit |
|
Exit abruptly from a function or procedure |
For |
|
Starts a loop that executes a finite number of times |
Goto |
|
Forces a jump to a label, regardless of nesting |
Repeat |
|
Repeat statements until a ternmination condition is met |
RunError |
|
Terminates the program with an error dialog |
While |
|
Repeat statements whilst a continuation condition is met |
Abort |
|
Aborts the current processing with a silent exception |
|
|
|
Example code : Breaking out of a loop for good reason |
var
i : Integer;
s : string;
begin
s := '';
// A big loop
for i := 1 to 10 do
begin
s := s + IntToStr(i) + ' '; // Exit loop when a certain condition is met
if Random(4) = 2 then Break;
end;
ShowMessage('i = '+IntToStr(i));
ShowMessage('s = '+s);
end;
|
Show full unit code |
i = 6
s = 1 2 3 4 5 6
|
|