Visual Basic Language Reference |
|
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 }
Parts
- Do
- Immediately exits the Do loop in which it appears. Execution continues with the statement following the Loop statement. Exit Do can be used only inside a Do loop. When used within nested Do loops, Exit Do transfers control to the loop that is one nested level above the loop where Exit Do occurs.
- For
- Immediately exits the For loop in which it appears. Execution continues with the statement following the Next statement. Exit For can be used only inside a For...Next or For Each...Next loop. When used within nested For loops, Exit For transfers control to the loop that is one nested level above the loop where Exit For occurs.
- Function
- Immediately exits the Function procedure in which it appears. Execution continues with the statement following the statement that called the Function procedure. Exit Function can be used only inside a Function procedure.
- Property
- Immediately exits the Property procedure in which it appears. Execution continues with the statement that called the Property procedure, that is, with the statement requesting or setting the property's value. Exit Property can be used only inside a Property procedure.
- Select
- Immediately exits the Select Case in which it appears. Execution continues with the statement following the End Select statement. Exit Select can be used only inside a Select Case statement.
- Sub
- Immediately exits the Sub procedure in which it appears. Execution continues with the statement following the statement that called the Sub procedure. Exit Sub can be used only inside a Sub procedure.
- Try
- Immediately exits the Try or Catch block in which it appears. Execution continues with the Finally block if there is one, or with the statement following the End Try statement otherwise. Exit Try can be used only inside a Try?Catch?Finally statement.
- While
- Immediately exits the While loop in which it appears. Execution continues with the statement following the End While statement. Exit While can be used only inside a While loop. When used within nested While loops, Exit While transfers control to the loop that is one nested level above the loop where Exit While occurs.
Remarks
Do not confuse Exit statements with End statements. Exit does not define the end of a statement.
Example
This example uses the Exit statement to exit a For...Next loop, a Do...Loop, and a Sub procedure.
Sub ExitStatementDemo()
Dim I, MyNum As Integer
Do ' Set up infinite loop.
For I = 1 To 1000 ' Loop 1000 times.
MyNum = Int(Rnd * 1000) ' Generate random numbers.
Select Case MyNum ' Evaluate random number.
Case 7: Exit For
' If 7, exit For...Next.
Case 29: Exit Do
' If 29, exit Do...Loop.
Case 54: Exit Sub
' If 54, exit Sub procedure.
End Select
Next I
Loop
End Sub
See Also
Do...Loop Statements | End Statement | For Each...Next Statements | For...Next Statements | Function Statement | Stop Statement | Sub Statement | Try...Catch...Finally Statements | For...Next Statements (Conceptual) | For Each...Next Statements (Conceptual)