Description |
The Goto keyword forces a jump to a given label.
It should Never be used in modern code since it makes code very difficult to maintain.
It is mostly used to force a termination of heavily nested code, where the logic to safely exit would be tortuous.
Never jump into or out of try statements, or into a loop or conditional block.
Be careful!
|
|
Notes |
Use with extreme caution and when fully justified.
|
|
Related commands |
Break |
|
Forces a jump out of a single loop |
Continue |
|
Forces a jump to the next iteration of a loop |
Exit |
|
Exit abruptly from a function or procedure |
Halt |
|
Terminates the program with an optional dialog |
RunError |
|
Terminates the program with an error dialog |
Abort |
|
Aborts the current processing with a silent exception |
|
|
|
|
Example code : |
// Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses SysUtils, Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} // Include form definitions procedure TForm1.FormCreate(Sender: TObject); var
i : Integer;
label
GotoLabel;
begin
for i := 1 to 10 do
begin
ShowMessage('i = '+IntToStr(i));
if i = 4 then Goto GotoLabel; // Conditionally exit the loop
end;
ShowMessage('The loop finished OK');
GotoLabel:
ShowMessage('Loop finished with i = '+IntToStr(i));
end; end.
|
Hide full unit code |
i = 1
i = 2
i = 3
i = 4
Loop finished with i = 4
|
|