In visual basic, the 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.
Following is the syntax of defining the Exit statement in a visual basic programming language.
In our applications, we can use Exit
statement with Do, For, Function, Property, etc. to stop or terminate the execution of a particular loop or statement based on our requirements.
Following is the pictorial representation of Exit
statement process flow in a visual basic programming language.
Now, we will see how to use Exit
statement in for loop, while loop, do-while loop to terminate the execution of loops in a visual basic programming language with examples.
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.
If you observe the above code, we used the Exit statement to terminate the for loop whenever the variable i value equals 3.
When we execute the above visual basic program, we will get the result as shown below.
If you observe the above result, whenever the variable i value equals 3, the for loop execution automatically stops.
This is how we can use the Exit
statement in for loop to terminate the execution of for loop based on our requirements.
In visual basic, we can exit or terminate the execution of the while loop immediately by using Exit
keyword.
Following is the example of using Exit
keyword in a while loop to terminate loop execution in a visual basic programming language.
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 as shown below.
This is how we can use the Exit
statement with the while loop to terminate the execution of the loop based on our requirements.
In visual basic, we can exit or terminate the execution of the do-while loop immediately by using Exit
keyword.
Following is the example of using Exit
keyword in a do-while loop to terminate loop execution in a visual basic programming language.
If you observe the above example, whenever the variable (i) value become 2, we are terminating the loop using Exit
statement.
We will get the following result when we execute the above visual basic program.
This is how we can use the Exit
statement in the do-while loop to terminate the execution of the 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.