Difference between revisions of "Goto"

From Free Pascal wiki
m (Fixed syntax highlighting; deleted category included in page template)
(remove “back to”-link since there are many pages linking here; re-arranged information; substituted legacy syntaxhighlight syntax; add Category: Code)
 
Line 1: Line 1:
 
{{goto}}
 
{{goto}}
  
 +
<syntaxhighlight inline lang="pascal">goto</syntaxhighlight> is an unconditional jump to a previously declared [[Label|<syntaxhighlight inline lang="pascal">label</syntaxhighlight>]] (either before or after the <syntaxhighlight inline lang="pascal">goto</syntaxhighlight> command).
 +
It is a [[Reserved words|reserved word]].
  
Go back to [[Reserved words]].
+
Usage of <syntaxhighlight inline lang="pascal">goto</syntaxhighlight> in high-level programming languages such as Pascal is highly discredited, since control structures of all sorts are available.
  
 
+
The last situation a <syntaxhighlight inline lang="pascal">goto</syntaxhighlight> is agreed with bad grace to be a significant system error, where a “graceful exit” is better than causing a system breakdown.
'''goto''' is a reserved word.
 
 
 
<syntaxhighlight enclose="none" lang="pascal">goto</syntaxhighlight> is an unconditional jump to a previously declared [[Label|<syntaxhighlight enclose="none" lang="pascal">label</syntaxhighlight>]] (either before or after the <syntaxhighlight enclose="none" lang="pascal">goto</syntaxhighlight> command).
 
 
 
Usage of <syntaxhighlight enclose="none" lang="pascal">goto</syntaxhighlight> in high-level programming languages such as Pascal is highly discredited, since control structures of all sorts are available.
 
 
 
The last situation a <syntaxhighlight enclose="none" lang="pascal">goto</syntaxhighlight> is agreed with bad grace to be a significant system error, where a “graceful exit” is better than causing a system breakdown.
 
  
 
As an example, here an excerpt from FPC's code base [https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/rtl/inc/extres.inc?revision=37113&view=markup#l324 <tt>rtl/inc/extres.inc</tt>]:
 
As an example, here an excerpt from FPC's code base [https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/rtl/inc/extres.inc?revision=37113&view=markup#l324 <tt>rtl/inc/extres.inc</tt>]:
Line 30: Line 25:
 
   ExitNoErr:
 
   ExitNoErr:
 
end;</syntaxhighlight>
 
end;</syntaxhighlight>
According to the value of {{Doc|package=RTL|unit=system|identifier=returnnilifgrowheapfails|text=<syntaxhighlight enclose="none" lang="pascal">returnNilIfGrowHeapFails</syntaxhighlight>}} {{Doc|package=RTL|unit=system|identifier=getmem|text=<syntaxhighlight enclose="none" lang="pascal">getMem</syntaxhighlight>}} possibly may return [[Nil|<syntaxhighlight enclose="none" lang="pascal">nil</syntaxhighlight>]].
+
According to the value of {{Doc|package=RTL|unit=system|identifier=returnnilifgrowheapfails|text=<syntaxhighlight inline lang="pascal">returnNilIfGrowHeapFails</syntaxhighlight>}} {{Doc|package=RTL|unit=system|identifier=getmem|text=<syntaxhighlight inline lang="pascal">getMem</syntaxhighlight>}} possibly may return [[Nil|<syntaxhighlight inline lang="pascal">nil</syntaxhighlight>]].
Instead of placing ''everything'' in a “success”-branch, a couple <syntaxhighlight enclose="none" lang="pascal">goto</syntaxhighlight> instructions were chosen.
+
Instead of placing ''everything'' in a “success”-branch, a couple <syntaxhighlight inline lang="pascal">goto</syntaxhighlight> instructions were chosen.
  
 
== See also ==
 
== See also ==
  
* [[sGoto|<syntaxhighlight lang="pascal" enclose="none">{$goto}</syntaxhighlight> compiler directive]]
+
* [[sGoto|<syntaxhighlight lang="pascal" inline>{$goto}</syntaxhighlight> compiler directive]]
* {{Doc|package=RTL|unit=system|identifier=longjmp|text=<syntaxhighlight lang="pascal" enclose="none">system.longJmp</syntaxhighlight>}}
+
* {{Doc|package=RTL|unit=system|identifier=longjmp|text=<syntaxhighlight lang="pascal" inline>system.longJmp</syntaxhighlight>}}
 +
 
 +
[[Category: Code]]

Latest revision as of 02:34, 6 May 2020

Deutsch (de) English (en) français (fr) русский (ru)

goto is an unconditional jump to a previously declared label (either before or after the goto command). It is a reserved word.

Usage of goto in high-level programming languages such as Pascal is highly discredited, since control structures of all sorts are available.

The last situation a goto is agreed with bad grace to be a significant system error, where a “graceful exit” is better than causing a system breakdown.

As an example, here an excerpt from FPC's code base rtl/inc/extres.inc:

324 procedure InitResources;
328 label ExitErrMem, ExitErrFile, ExitNoErr;
329 begin
339   ResHeader:=GetMem(sizeof(TExtHeader));
340   if ResHeader=nil then goto ExitErrFile;
366   goto ExitNoErr;
367 
368   ExitErrMem:
369     FreeMem(ResHeader);
370     ResHeader:=nil;
371   ExitErrFile:
372     {$I-}
373     Close(fd);
374     {$I+}
375   ExitNoErr:
376 end;

According to the value of returnNilIfGrowHeapFails getMem possibly may return nil. Instead of placing everything in a “success”-branch, a couple goto instructions were chosen.

See also