Visual Basic Exit Statement

In visual basic, Exit statement is useful to terminate the execution of loops (for, while, do-while, etc.) and transfers the control immediately to the next statements that follow a terminated loops or statements.

 

In visual basic, we can also use Exit statement in nested loops to stop or terminate the execution of inner loops based on our requirements.

Visual Basic Exit Statement

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

 

Exit Do | For | Function | Property | Select | Sub | Try | While } 

In our applications, we can use Exit statement with Do, For, Function, Property, etc. to stop or terminate the execution of particular loop or statement based on our requirements.

Visual Basic Break Statement Flow Chart

Following is the pictorial representation of Exit statement process flow in visual basic programming language.

 

Visual Basic Exit Statement Flow Chart Diagram

 

Now, we will see how to use Exit statement in for loop, while loop, do-while loop to terminate the execution of loops in visual basic programming language with examples.

Visual Basic For Loop with Exit Statement

In visual basic, by using Exit keyword we can stop the execution of for loop statement based on our requirements.

 

Following is the example of terminating the execution of for loop with Exit statement.

 

Module Module1

    Sub Main()

        For i As Integer = 1 To 4

            If i = 3 Then Exit For

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

        Next

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

        Console.ReadLine()

    End Sub

End Module

If you observe the above code, we used Exit statement to terminate the for loop whenever the variable i value equals to 3.

 

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

 

Visual Basic For Loop with Exit Statement Example Result

 

If you observe the above result, whenever the variable i value equals to 3, automatically the for loop execution has stopped.

 

This is how we can we use the Exit statement in for loop to terminate the execution of for loop based on our requirements.

Visual Basic While Loop with Exit Statement

In visual basic, we can exit or terminate the execution of while loop immediately by using Exit keyword.

 

Following is the example of using Exit keyword in while loop to terminate the execution of loop in visual basic programming language.

 

Module Module1

    Sub Main()

        Dim i As Integer = 1

        While i < 4

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

            i += 1

            If i = 2 Then Exit While

        End While

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

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, whenever the variable (i) value become 2 we are terminating the while loop using Exit statement.

 

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

 

Visual Basic While Loop with Exit Statement Example Result

 

This is how we can use the Exit statement with while loop to terminate the execution of loop based on our requirements.

Visual Basic Do-While Loop with Exit Statement

In visual basic, we can exit or terminate the execution of do-while loop immediately by using Exit keyword.

 

Following is the example of using Exit keyword in do-while loop to terminate execution of loop in visual basic programming language.

 

Module Module1

    Sub Main()

        Dim i As Integer = 1

        Do

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

            i += 1

            If i = 2 Then Exit Do

        Loop While i < 4

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

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, whenever the variable (i) value become 2 we are terminating the loop using Exit statement.

 

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

 

Visual Basic Do While Loop with Exit Statement Example Result

 

This is how we can use the Exit statement in do-while loop to terminate the execution of loop based on our requirements.

 

This is how we can use the Exit statement in our visual basic applications to terminate the execution of loops or statements based on our requirements.

Topics Covered