Exit Statement (Visual Basic)
Visual Studio 2012
Exits a procedure or block and transfers control immediately to the statement following the procedure call or the block definition.
Exit { Do | For | Function | Property | Select | Sub | Try | While }
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.
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
The following example assigns the return value to the function name myFunction, and then uses Exit Function to return from the function.
Function myFunction(ByVal j As Integer) As Double myFunction = 3.87 * j Exit Function End Function
The following example uses the Return Statement (Visual Basic) to assign the return value and exit the function.
Function myFunction(ByVal j As Integer) As Double Return 3.87 * j End Function