Back to index
|
Exit Statement
Exits a procedure or block and transfers control immediately to the statement following the procedure call or the block definition.
|
Format:
Exit { Do | For | Function | Select | Sub | While }
Syntax:
- 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.
- 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.
- 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:
1. Do not confuse Exit statements with End statements. Exit does not define the end of a statement.
See also:
- Do...Loop statements
- For...Next statements
- Function statements
- Stop statement
- Sub statement
Example:
Sub ExitStatementDemo()
Do ' Set up infinite loop.
For I = 1 To 1000 ' Loop 1000 times.
MyNum = Int(Rnd * 100) ' 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
Copyright (c) 2000-2006 by Smartphoneware. All rights reserved.