Declare labels like this:
label
label;
You can declare several labels at once:
label
label1, ...,
labeln;
A label can be any valid identifier or any numeral between 0 and 9999.
The label declaration, marked statement, and goto statement must belong to the same block. (See Blocks and Scope,
below.) Hence it is not possible to jump into or out of a procedure or function. Do not mark more than one statement
in a block with the same label.
For example,
label StartHere;
...
StartHere: Beep;
goto StartHere;
creates an infinite
loop that calls the
Beep
procedure repeatedly.
Additionally, it is not possible to jump into or out of a try-finally or try-except statement.
The goto statement is generally discouraged in structured programming. It is, however, sometimes used as a way
of exiting from nested loops, as in the following example.
procedure FindFirstAnswer;
var X, Y, Z, Count: Integer;
label FoundAnAnswer;
begin
Count := SomeConstant;
for X := 1
to Count do
for Y := 1 to Count do
for Z := 1 to Count do
if ... {
some condition holds on X, Y, and Z } then
goto FoundAnAnswer;
... { Code to execute if no answer is found }
Exit;
FoundAnAnswer:
... { Code to execute when an answer is found }
end;
Notice that we are using goto to jump out of a nested loop. Never jump into a loop
or other structured statement,
since this can have unpredictable effects.
Dostları ilə paylaş: