Imperative programs
conditional structures
loops
local jumps
This task is to demonstrate a local jump and a global jump and the various other types of jumps that the language supports.
For the purpose of this task, the jumps need not be used for a single purpose and you have the freedom to use these jumps for different purposes.
You may also defer to more specific tasks, like Exceptions or Generator.
This task provides a "grab bag" for several types of jumps. There are non-local jumps across function calls, or long jumps to anywhere within a program. Anywhere means not only to the tops of functions!

 Some languages can go to any global label in a program.
 Some languages can break multiple function calls, also known as unwinding the call stack.
 Some languages can save a continuation. The program can later continue from the same place. So you can jump anywhere, but only if you have a previous visit there (to save the continuation).

These jumps are not all alike.
A simple goto never touches the call stack.
A continuation saves the call stack, so you can continue a function call after it ends.



Task

Use your language to demonstrate the various types of jumps that it supports.

Because the possibilities vary by language, this task is not specific.
You have the freedom to use these jumps for different purposes.
You may also defer to more specific tasks, like Exceptions or Generator.




' FB 1.05.0 Win64
 
' compiled with -lang fb (the default) and -e switches
 
Sub MySub()
  Goto localLabel
  Dim a As Integer = 10  '' compiler warning that this variable definition is being skipped
  localLabel:
  Print "localLabel reached"
  Print "a = "; a  '' prints garbage as 'a' is uninitialized
End Sub
 
Sub MySub2()
  On Error Goto handler
  Open "zzz.zz" For Input As #1 '' this file doesn't exist!
  On Error Goto 0 '' turns off error handling
  End
 
  handler:
  Dim e As Integer = Err '' cache error number before printing message
  Print "Error number"; e; " occurred - file not found"
End Sub
 
Sub MySub3()
  Dim b As Integer = 2
  On b Goto label1, label2 '' jumps to label2
 
  label1:
  Print "Label1 reached"
  Return '' premature return from Sub
 
  label2:
  Print "Label2 reached"
End Sub
 
Sub MySub4()
  Dim c As Integer = 3
  If c = 3 Goto localLabel2 '' better to use If ... Then Goto ... in new code
  Print "This won't be seen"
  localLabel2:
  Print "localLabel2 reached"
End Sub
 
MySub
MySub2
MySub3
MySub4
Print
Print "Pres any key to quit"
Print