You are on page 1of 1

Docs Documentation Learn Q&A Code Samples / Search Sign in

.NET Languages S Workloads S APIs S Resources S Download .NET

Docs ​/ .NET ​/ Visual Basic guide ​/ Language reference # Bookmark D Feedback / Edit + Share

. Filter by title Exit Statement (Visual Basic) Is this page helpful?

07/20/2015 • 3 minutes to read • +6 ) Yes * No


Visual Basic Guide

What's New for Visual Basic


Exits a procedure or block and transfers control immediately to the statement following the procedure call In this article
Breaking changes in the compiler or the block definition. Syntax

T Get Started
Statements
Developing Applications
Syntax


Remarks

T Programming Concepts Example

T Program Structure and Code Conventions Example
VB = Copy

T Language Features Example
Exit { Do | For | Function | Property | Select | Sub | Try | While } See also

T COM Interop

Language Reference

Statements
Language Reference

Configure language version

Typographic and Code Conventions Exit Do


Visual Basic Runtime Library Members Immediately exits the Do loop in which it appears. Execution continues with the statement following the

T Keywords Loop statement. Exit Do can be used only inside a Do loop. When used within nested Do loops, Exit

Attributes Do exits the innermost loop and transfers control to the next higher level of nesting.

Constants and Enumerations


Exit For

T Data Type Summary
Immediately exits the For loop in which it appears. Execution continues with the statement following the
Directives Next statement. Exit For can be used only inside a For ... Next or For Each ... Next loop. When used


T Functions within nested For loops, Exit For exits the innermost loop and transfers control to the next higher level

T Modifiers of nesting.
Modules
Exit Function
Nothing
Immediately exits the Function procedure in which it appears. Execution continues with the statement

T Objects
following the statement that called the Function procedure. Exit Function can be used only inside a

T Operators
Function procedure.
Properties

T Queries To specify a return value, you can assign the value to the function name on a line before the Exit
Function statement. To assign the return value and exit the function in one statement, you can instead use
Statements

the Return Statement.


Statements

A-E Statements Exit Property



A-E Statements Immediately exits the Property procedure in which it appears. Execution continues with the statement
AddHandler Statement that called the Property procedure, that is, with the statement requesting or setting the property's value.
Call Statement Exit Property can be used only inside a property's Get or Set procedure.

Class Statement
To specify a return value in a Get procedure, you can assign the value to the function name on a line
Const Statement
before the Exit Property statement. To assign the return value and exit the Get procedure in one
Continue Statement statement, you can instead use the Return statement.
Declare Statement
In a Set procedure, the Exit Property statement is equivalent to the Return statement.
Delegate Statement

Dim Statement Exit Select


Do...Loop Statement Immediately exits the Select Case block in which it appears. Execution continues with the statement
Else Statement following the End Select statement. Exit Select can be used only inside a Select Case statement.
End Statement
Exit Sub
End <keyword> Statement
Immediately exits the Sub procedure in which it appears. Execution continues with the statement following
Enum Statement
the statement that called the Sub procedure. Exit Sub can be used only inside a Sub procedure.
Erase Statement

Error Statement In a Sub procedure, the Exit Sub statement is equivalent to the Return statement.

Event Statement
Exit Try
Exit Statement
Immediately exits the Try or Catch block in which it appears. Execution continues with the Finally block

T F-P Statements if there is one, or with the statement following the End Try statement otherwise. Exit Try can be used

T Q-Z Statements only inside a Try or Catch block, and not inside a Finally block.

T Clauses
Exit While
Declaration Contexts and Default
Immediately exits the While loop in which it appears. Execution continues with the statement following the
Access Levels
End While statement. Exit While can be used only inside a While loop. When used within nested
Attribute List
While loops, Exit While transfers control to the loop that is one nested level above the loop where Exit
Parameter List
While occurs.
Type List

T XML Comment Tags

T XML Axis Properties Remarks

T XML Literals
Do not confuse Exit statements with End statements. Exit does not define the end of a statement.

T Error Messages

T Reference

Sample Applications Example


Walkthroughs
In the following example, the loop condition stops the loop when the index variable is greater than 100.
The If statement in the loop, however, causes the Exit Do statement to stop the loop when the index
variable is greater than 10.

VB = Copy

Dim index As Integer = 0


Do While index <= 100
If index > 10 Then
Exit Do
End If

Debug.Write(index.ToString & " ")


index += 1
Loop

Debug.WriteLine("")
' Output: 0 1 2 3 4 5 6 7 8 9 10

Example
The following example assigns the return value to the function name myFunction , and then uses Exit
Function to return from the function:

VB = Copy

Function MyFunction(ByVal j As Integer) As Double


MyFunction = 3.87 * j
Exit Function
End Function

Example
The following example uses the Return Statement to assign the return value and exit the function:

VB = Copy

Function MyFunction(ByVal j As Integer) As Double


Return 3.87 * j
End Function

See also
Continue Statement
Do...Loop Statement
End Statement
For Each...Next Statement
For...Next Statement
Function Statement
Return Statement
Stop Statement
Sub Statement
Try...Catch...Finally Statement

Feedback

Submit and view feedback for

This product 6 This page

6 View all page feedback

! English (United States) 0 Theme Previous Version Docs Blog Contribute Privacy & Cookies Terms of Use Trademarks © Microsoft 2021
- Download PDF

You might also like