DelphiBasics
Break
Procedure
Forces a jump out of a single loop System unit
 procedure Break();
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
ContinueForces a jump to the next iteration of a loop
ExitExit abruptly from a function or procedure
ForStarts a loop that executes a finite number of times
GotoForces a jump to a label, regardless of nesting
RepeatRepeat statements until a ternmination condition is met
RunErrorTerminates the program with an error dialog
WhileRepeat statements whilst a continuation condition is met
AbortAborts the current processing with a silent exception
 Download this web site as a Windows program.




 
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
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page