Name
Goto Keyword
Syntax
goto LabelDescription
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
gotostatement 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
gotostatement is to jump out of deeply nested loops. When it is used in this way, you can think of it as a super-Breakstatement.The entire VCL source code contains only two
gotostatements. Both are used to break out of a nested loop. Consider this a guideline for the proper use of thegotostatement.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-exceptortry-finallystatement.Unlike standard Pascal, Delphi does not permit a jump from a nested subroutine into an outer subroutine. Use the
Exitprocedure 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; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access