Goto / Return
Goto
Syntax
Goto <label>
' …
<label>:
' target statements
Description
- Jumps unconditionally to the named
<label>: elsewhere in the same procedure. - Labels are identifiers followed by a colon.
- Use sparingly—overuse can make code hard to follow.
Example
Dim n As Integer = 0
LoopStart:
If n >= 3 Then
GoTo EndLoop
End If
Print("n = " + n.ToString)
n += 1
GoTo LoopStart
EndLoop:
Print("Done looping.")
Return
Syntax (in Functions or Subs)
Description
- Immediately exits the current
Sub or Function. - In a
Function, optional <expression> supplies the return value.
Example
Function Factorial(n As Integer) As Integer
If n < 2 Then Return 1
Return n * Factorial(n - 1)
End Function
Print("5! = " + str(Factorial(5))) ' Outputs 5! = 120