GosubGoto

=Goto and Gosub= toc

Branch Labels
The mechanism that Run BASIC uses to mark places that we can goto is called a branch label. There are two ways to define a branch label in Run BASIC. You can use any valid integer number as a branch label, or you can use an easier to remember type which uses letters.

Examples of integer branch labels: code 10 150 75 900 5400 code

Examples of alphanumeric (using letters and numbers) branch labels: code [start] [loopBack] [getResponse] [point1] code

Alphanumeric branch lables appear in square brackets, consist of letters and numbers, and do not contain spaces.

Goto
The **Goto** statement causes program execution to continue at the code immediately after the branch label specified. The syntax is as follows:

GOTO label //- Transfer control to the code immediately after label//

Here's an example that uses an integer branch label:

code goto 567 code

Here's an example that uses an alphanumeric branch label:

code goto [doAgain] code

Alphanumeric Branch Labels
Alphanumeric branch labels are preferred by most programmers because they can (and should be) descriptive. It is an easy way to have self-documented code. It is easy to understand and remember descriptive branch labels like [doAgain], [CountTotal], and [OpenFile].

The following program prints a message, then uses **Goto** to transfer execution to the branch label **[goodbye]** Because program execution jumps to the specified branch label, the code following the **Goto** statement is not executed.

code format="vbnet" 'alphanumeric branch label demo print "Hello, World!" goto [goodbye]

'this line is not executed: print "I'm here."

'this is a branch label: [goodbye] print "Goodbye!" code

Integer Branch Labels
Integer branch labels function in exactly the same way as alphanumeric branch labels. Here is the code above, with integer branch labels instead of alphanumeric branch labels.

code format="vbnet" 'integer branch label demo 'disply a message: print "Good morning!" goto 134

'this line is not executed: print "Good afternoon."

'this is a branch label: 134 print "Good evening!" code

New Line
It is not necessary to place the line of code immediately following a branch label on a new line. All of the following are acceptable.

code format="vbnet" 134 print "Good evening!" 135 print "Good night!" [goodbye] print "Goodbye!" [goodnight] print "Good night!" code

Gosub

 * Gosub** is different than **Goto** because it branches to another section of code, then returns and executes the code following the **Gosub** statement. //The Goto statement does not cause execution to return to the code following the **Goto** statement.//

The syntax is as follows:

GOSUB label //- Transfer control to the code immediately after label (See RETURN)//

It is important that program code not "fall into" a branch label that begins a **Gosub** routine. You may use **Wait** to halt execution.

code print "Stuff" WAIT          'halt execution before the branch label for the GOSUB

[BranchLabel] 'this branch label will be called with GOSUB print "Hello" return code

Return
A block of code that is accessed by the **Gosub** statement MUST end with a **Return** statement. **Return** causes execution to return to the line of code following the **Gosub** statement.

The syntax is as follows:

RETURN //- Resume execution after the last GOSUB statement//

Here is a demonstration of **Gosub** and **Return**. Note that the program returns to the statement following the **Gosub** statement and prints "I'm done for now."

code format="vbnet" 'gosub demo 'display a new message print "Run BASIC is great!"

'gosub to a different spot in the code gosub [more]

'execution returns here after the gosub print "I'm done for now!"

'do not allow code to fall into a gosub routine: wait

'this label begins a subroutine, which ends with RETURN [more] print "I love Run BASIC!" return code

Integer branch labels can be used with **Gosub**. The following demo is identical to the one above, but it uses integer branch labels.

code format="vbnet" 'gosub demo 'display a new message print "Run BASIC is great!"

'gosub to a different spot in the code gosub 211 'execution returns here after the gosub print "I'm done for now!"

'do not allow code to fall into a gosub routine: wait

'this label begins a subroutine, which ends with RETURN 211 print "I love Run BASIC!" return code