vbnet
  1. vbnet-exit-statement

Control Statements in VB.NET: Exit Statement

In VB.NET, the Exit statement is used to immediately exit a block of code, such as a loop or a function. In this page, we will discuss the syntax, example, output, explanation, use, important points, and summary of the Exit statement in VB.NET.

Syntax

The syntax for the Exit statement in VB.NET is as follows:

Exit While
Exit Do
Exit For
Exit Function
Exit Sub

Here is a brief description of each of these Exit statements:

  • Exit While: Immediately exits a While loop and continues execution after the loop.
  • Exit Do: Immediately exits a Do loop and continues execution after the loop.
  • Exit For: Immediately exits a For loop and continues execution after the loop.
  • Exit Function: Immediately exits a function and returns control to the calling routine.
  • Exit Sub: Immediately exits a subroutine and returns control to the calling routine.

Example

Here's an example of using the Exit statement in a While loop:

Dim i As Integer = 1

While i < 10
    Console.WriteLine(i)
    If i = 5 Then
        Exit While
    End If
    i += 1
End While

In this example, the loop will exit when i equals 5.

Output

The output of the above code will be:

1
2
3
4
5

Explanation

The Exit statement in VB.NET is used to immediately exit a block of code, such as a loop or a function. When the Exit statement is encountered, control transfers immediately to the statement following the loop or function call that was exited.

Use

The Exit statement is useful when you need to abruptly exit a loop or a function in VB.NET. It allows you to stop the execution of the code at a specific point and continue from another point.

Important Points

  • The Exit statement is used to immediately exit a block of code, such as a loop or a function.
  • There are different types of Exit statements depending on the type of block you want to exit from.
  • When the Exit statement is encountered, control transfers immediately to the statement following the loop or function call that was exited.

Summary

In this page, we discussed the Exit statement in VB.NET, its syntax, example, output, explanation, use, and important points. The Exit statement is useful when you need to abruptly exit a loop or a function in VB.NET, and it allows you to stop the execution of the code at a specific point and continue from another point.

Published on: