Name
Goto Keyword
Syntax
goto Label
Description
The goto
statement transfers control to the given
label. The label can be any identifier or a digit string with up to
four digits. You cannot use an identifier used as a label as a local
variable in the same block.
Tips and Tricks
Indiscriminate use of the
goto
statement results in the classic problem of “spaghetti” code, that is, source code that is as tangled as a plate of spaghetti.The most common use for the
goto
statement is to jump out of deeply nested loops. When it is used in this way, you can think of it as a super-Break
statement.The entire VCL source code contains only two
goto
statements. Both are used to break out of a nested loop. Consider this a guideline for the proper use of thegoto
statement.Jumping from outside a block (loop, conditional, or nested subroutine) into the block has unpredictable results. Delphi does not permit a jump into or out of any part of a
try
-except
ortry
-finally
statement.Unlike standard Pascal, Delphi does not permit a jump from a nested subroutine into an outer subroutine. Use the
Exit
procedure to return prematurely from a function or procedure.
Example
// Find the first non-blank cell in a grid, and return its // contents, converted to all uppercase. If the entire grid // is blank, return a default string. Skip over the fixed // rows and columns because they are just identifying headers. procedure UpcaseCell(Grid: TStringGrid; const Default: string): string; var R, C: Integer; label NestedBreak; ...
Get Delphi in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.