VB.NET GoTo Statement

In VB.NET, the GoTo statement is known as a jump statement. It is a control statement that transfers the flow of control to the specified label within the procedure. The GoTo statement uses labels that must be a valid identifier. The GoTo statement can be used in Select case, decision control statements, and loops.

Syntax:

Here, GoTo is a keyword, and label_1 is a label used to transfer control to a specified label statement in a program.

Now we will see how to use the GoTo statement in the loop, select case, or decision-making statement to transfer control to the specified label statement in the VB.NET program.

Use of GoTo statement in If Else

Example 1: Write a simple program to print whether the number is even or odd in GoTo Statement.

Goto_Statement.vb

Output:

VB.NET GoTo Statement

In the above program, when we enter a number, it checks whether the number is even or odd. And if the number is odd, the GoTo statement will be encountered, and the control transfers to the odd statement Else the control transfer to the even statement.

Use of GoTo Statement in For Next loop

Example 2: Write a program to print the sum of the First ten numbers using the Goto Statement in VB.NET.

Goto_For.vb

Output:

VB.NET GoTo Statement

In the above program, the For loop is executed till the given condition ( i = 1 To 10). And when the value of i is equal to 5, the GoTo statement will be encountered, and it transfers the control to total_sum so that the total sum of each iteration can be printed in the For loop.

Use of GoTo statement in Select Case Statement

Example 3: Write a simple program to print the days' names in select cases using the GoTo Statement.

Goto_Select.vb

Output:

VB.NET GoTo Statement

In the Select case statement, the value of Days Thurs will compare the values of all selected cases available in a program. If it matches any statement, it prints the particular statement and contains the GoTo statement that transfers control to defined case1: and then the following statement is executed.

Use of GoTo statement in While End Loop Statement

Example 4: Write a simple program to understand the use of GoTo statement in the While loop.

Goto_While.vb

Output:

VB.NET GoTo Statement
Next TopicVB.NET Enum