← Documentation Home

Labels and Goto

Labels let you name a location in code. Goto jumps to that location. LunarBasic supports labels, but it also checks that jumps stay inside valid scope.

Back to Language Reference


Basic Syntax

<labelName>: Goto <labelName>
  • Start:
  • Goto Start

Important Rules

  • Labels are standalone statements.
  • Goto requires an identifier target.
  • Labels and gotos are matched case-insensitively.
  • Labels must be unique inside the current top-level program, Function, or Sub.
  • A Goto may only target a label in the same lexical scope or an enclosing lexical scope.

What Scope Means

Scope means either the top-level program or the body of the current Function or Sub. Nested blocks such as If, loops, and Select Case branches also affect where a Goto is allowed to jump.

  • Top-level labels are only for top-level code.
  • Labels inside a Function are only visible inside that same Function.
  • Labels inside a Sub are only visible inside that same Sub.
  • You can jump to a label in the same block or an enclosing block.
  • You cannot jump into a nested block from outside it.
  • You cannot jump from one sibling nested block into another.

Examples

  • Allowed: from inside a For loop to a label in the surrounding Function
  • Allowed: from inside an If block to a label in the containing top-level program
  • Not allowed: from outside a For loop to a label declared inside that loop
  • Not allowed: from one Select Case branch to a label declared in another branch