Visual Basic GoTo Statement

In visual basic, GoTo statement is useful to transfer the program control to the specified labeled statement and it is useful to get out of the loop or exit from a deeply nested loops based on our requirements.

 

Generally, in visual basic the defined labeled statement must always exists in the scope of GoTo statement and we can also define the multiple GoTo statements in our application to transfer the program control to the specified labeled statement.

 

For example, we can use GoTo statement in Select statement to transfer a control from one Select-Case label to another or to the default label based on our requirements.

Visual Basic Goto Statement Syntax

Following is the syntax of defining the GoTo statement in visual basic programming language.

 

GoTo labeled_statement; 

If you observe the above syntax, we defined GoTo statement by using GoTo keyword and with labeled_statement. Here, the labeled_statement is used to transfer the program control to the specified labeled_statement position.

 

Now, we will see how to use GoTo statement in visual basic for loop to get out of the loop at particular condition with examples

Visual Basic Goto Statement with For Loop Example

Following is the example of using GoTo statement in for loop to move the program control to the specified label statement based on our requirements.

 

Module Module1

    Sub Main()

        For i As Integer = 1 To 10 - 1

            If i = 5 Then

                GoTo endloop

            End If

            Console.WriteLine("i value: {0}", i)

        Next

endloop:

        Console.WriteLine("The end")

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe above example, we used GoTo statement in for loop with labeled statement “endloop” to exit for loop and transfer the program execution to the defined label statement whenever the variable (i) value equals to 5.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Goto Statement with For Loop Example Result

 

If you observe the above result, whenever the variable (i) value is equals to 5, then the GoTo statement transferred the program control from for loop to the specified label statement (endloop) position.

Visual Basic Goto Statement with Select Statement

In visual basic, we can use GoTo statement to exit from the defined loops or transfer the control to the specific Select-Case label or to the default label in Select statement based on our requirements.

 

Now, we will see how to use GoTo statement in Select-Case statement with example. Following is the example of using GoTo with Select-Case statement to transfer the control from one Select-Case label to another based on our requirements.

 

Module Module1

    Sub Main()

        Dim i As Integer = 3, j As Integer = 0

        Select Case i

            Case 1

Case1:

                j += 20

                Console.WriteLine("j value is {0}", j)

            Case 2

                j += 5

                GoTo Case1

            Case 3

                j += 30

                GoTo Case1

            Case Else

                Console.WriteLine("Not Known")

        End Select

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we used GoTo statement in multiple Select cases and trying to transfer the program control from case 2 / case 3 to case 1.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Goto Statement with Select Case Statement Example Result

 

This is how we can use GoTo statement with Select-Case statements to transfer the program control from one case to another in visual basic programming language based on our requirements.

 

It’s better to avoid using GoTo statement in our visual basic applications because it will make the program logic more complex and it’s difficult to understand the process flow of program execution.