GoTo and GoSub

 

The GoTo Statement

 

The GoTo statement can be used to branch to statements within a Sub or Function procedure. We have all heard the lectures on the "evils" of using GoTo statements, so I'm not going to rehash all that here. In general, use of modern programming constructs such as the ones previously presented (Do/Loop, If/Then/Else, Select Case, etc.) virtually eliminate the need to use GoTos. You'll find that the only time that the use of GoTo is required is when setting up error-handling code (error-handling is discussed in a later topic).

 

The rules for using GoTo are as follows:

·         The destination of a GoTo statement must be a line label or line number that resides within the same Sub or Function procedure as the statement that issues the GoTo (i.e., you cannot "go to" a line in a Sub or Function other than the one you are currently in).

·         The name of the line label follows the same rules as that for naming a variable. The line label must begin in the first column of the line and must be followed by a colon(:).

·         If a line number is used, it must begin in the first column of the line.

 

Following is a version of the factorial program presented earlier that uses GoTos instead of the For/Next loop for looping :

 

Private Sub cmdTryIt_Click()

 

    Dim lngFactorial    As Long

    Dim intInputNbr     As Integer

    Dim intLoopCtr      As Integer

   

    intInputNbr = Val(InputBox("Enter a number:", "GoTo Demo"))

   

    lngFactorial = 1

    intLoopCtr = 1

 

Loop_Start:

    If intLoopCtr > intInputNbr Then GoTo 10

 

    lngFactorial = lngFactorial * intLoopCtr

    intLoopCtr = intLoopCtr + 1

    GoTo Loop_Start

   

10  ' End of loop

    Print CStr(intInputNbr); "! = "; lngFactorial

   

End Sub

 

Download the VB project code for the example above here.

 

 

The GoSub Statement

 

In earlier versions of BASIC, the only way you could make your programs modular was to break your program up into "subroutines" and use GoSub to execute that subroutine and return back to the calling statement. GoSub was included in VB to maintain backward compatibility; it is not a particularly "bad" construct to use, but its use is generally discouraged – it is recommended that Sub and Function procedures be used instead.

 

The rules for using GoSub are as follows:

·         The destination of a GoSub statement must be a line label or line number that resides within the same Sub or Function procedure as the statement that issues the GoSub (i.e., you cannot "GoSub" to a line in a Sub or Function other than the one you are currently in).  In effect, using GoSub lets you have "subs within a sub" (with the exception that you cannot pass parameters to a "GoSubbed" routine).

·         If a line label is used, the name of that line label must follow the same rules as that for naming a variable. The line label must begin in the first column of the line and must be followed by a colon(:).

·         If a line number is used, it must begin in the first column of the line.

·         Once the destination of the GoSub is reached, a Return statement will return control to the statement after the one that issued the GoSub.

 

Following is code to demonstrate the use of Gosub/Return. Note that the use of the Exit Sub statement is required to prevent "falling through".  The code in this VB Sub is set up in a manner similar to the way a program would have to be written in QBASIC (one of the ancestors of VB): the three GoSubs formulate the main control flow of the "program" and the Exit Sub ends the "program". Note: Without the Exit Sub, we would fall through to SubroutineA, and when the Return statement associated with SubroutineA hit, a "Return without GoSub" error would be generated.

 

Private Sub cmdTryIt_Click()

 

    GoSub SubroutineA

 

    GoSub SubroutineB

 

    GoSub 1000

 

    Exit Sub

 

SubroutineA:

    Print "Hey kids, I'm in Subroutine A"

    Return

 

SubroutineB:

    Print "Hey kids, I'm in Subroutine B"

    Return

 

1000

    Print "Hey kids, I'm in Subroutine 1000"

    Return

       

End Sub

 

Download the VB project code for the example above here.