Branch Label

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Not supported in Run BASIC

Description

A branch label "identifies" a portion of the code which can be accessed by GOTO and GOSUB statements. It is a reference label only, having no direct affect on the execution of code. A branch label encountered in the normal program flow will be skipped over as if it were a comment.
Branch labels also used for event handling - in GUI programming as control's events (window trapclose, button click etc), as timer event handler, and as ON ERROR GOTO destination.

Syntax

  1. goto [myBranch]
  2. gosub [ourBranch]

Hints

Just BASIC will accept line numbers as labels. This is useful when using old BASIC code. Then it would look like
goto 10
gosub 20
However, it is better to use alphanumeric ones, because they are descriptive. Spaces are not allowed in branch labels.
A branch label can only be defined once in a program, which means, you cannot use the same name for more than one branch. (You can, however, call that branch as many times as you like in your program code.)
There's an exception; branch labels within subs or functions may have the same name as a branch label in the main program or in another sub or function.
Branch labels used within subs or functions cannot be "seen" by the main program. Similarly, code within subs and functions cannot "see" the branch labels in the main program.
This is a result of what is called "scoping". Branch labels within subs and functions are local in scope and the main program is not aware of them and vice versa.
This concept can cause you untold frustration as you wonder why a statement in your sub to gosub a branch label in the main program tells you it can't find that particular branch label.
Also, similar thing (missing label) might happen if your program uses labels to handle events and event fires while program executes in a SUB/FUNCTION (likely only if you have WAIT in that SUB/FUNCTION).
See Variables for a discussion on scoping.

Example

nomainwin

statictext #1.st1 "When you close this window ";_
                    "the program will branch to ";_
                    "the branch label [quit], and ";_
                    "will continue program execution - ";_
                    "which will be to close the program.",_
                    20, 20, 200, 200

    open "Branch Label Demo" for window as #1
    #1 "trapclose [quit]"

    wait

[quit]
    close #1
    end

Useful Procedures

' Place a useful function using this keyword here