GOTO

Syntax

Syntax: GOTO label
Source: PB_STMT_GOTO handler (pb_codegen.c:6625)

Description

Transfers execution unconditionally to a specified label within the same function or subroutine. GOTO is commonly used for error handling, cleanup code, and skipping sections of code. Labels are defined by an identifier followed by a colon. The compiler generates a direct jmp instruction to the label's address.

Parameters

ParameterDescription
labelThe name of the target label (without the trailing colon). The label must be defined in the same FUNCTION or SUB.

Example

FUNCTION PBMAIN() AS LONG
    LOCAL x AS LONG
    
    x = -5
    
    ' Error handling with GOTO
    IF x < 0 THEN GOTO ErrorHandler
    
    PRINT "Value is OK: "; x
    GOTO Cleanup
    
ErrorHandler:
    PRINT "Error: negative value detected!"
    x = 0
    
Cleanup:
    PRINT "Finished with x = "; x
    FUNCTION = 0
END FUNCTION

Generated Assembly

; Direct GOTO:
jmp    __pb_label_<routine>_<labelname>

; Label definition:
__pb_label_<routine>_<labelname>:

Example Assembly Output

IF x < 0 THEN GOTO ErrorHandler
PRINT "OK"
GOTO Done
ErrorHandler:
PRINT "Error!"
Done:
PRINT "Finished"
; Generated assembly:
mov    rax, [rbp - x_offset]
cmp    rax, 0
jge    __pb_skip_N                    ; skip if x >= 0
jmp    __pb_label_PBMAIN_ErrorHandler
__pb_skip_N:
; PRINT "OK" code
jmp    __pb_label_PBMAIN_Done
__pb_label_PBMAIN_ErrorHandler:
; PRINT "Error!" code
__pb_label_PBMAIN_Done:
; PRINT "Finished" code