Goto (statement)

Syntax

Goto label

Description

Transfers execution to the line containing the specified label.

Comments

The compiler will produce an error if label does not exist.

The label must appear within the same subroutine or function as the Goto.

Labels are identifiers that follow these rules:

  1. Must begin with a letter.

  2. May contain letters, digits, and the underscore character.

  3.  Must not exceed 80 characters in length.

  4.  Must be followed by a colon (:).

Labels are not case-sensitive.

Example

This example gets a name from the user and then branches to a statement, depending on the input name. If the name is not MICHAEL, it is reset to MICHAEL unless it is null or the user clicks Cancel--in which case, the program displays a message and terminates.

Sub Main()
  uname$ = UCase(InputBox("Enter your name:","Enter Name"))
  If uname$ = "MICHAEL" Then
    Goto RightName
  Else
    Goto WrongName
  End If

WrongName:
  If (uname$ = "") Then
    MsgBox "No name? Clicked Cancel? I'm shutting down."
  Else
    MsgBox "I am renaming you MICHAEL!"
    uname$ = "MICHAEL"
    Goto RightName
  End If
  Exit Sub

RightName:
  MsgBox "Hello, " & uname$
End Sub

See Also

GoSub (statement); Call (statement).

Note:

To break out of an infinite loop, press Ctrl+Break.

More information

G