# Exit
{: .no_toc }

Exits a block of **Do…Loop**, **For…Next**, **While...Wend**, **Function**, **Sub**, or **Property** code.

Syntax:

- **Exit Do**  
  Provides a way to exit a **[Do...Loop](/tB/Core/Do-Loop.md)** statement. It can be used only inside a **Do...Loop** statement. **Exit Do** transfers control to the statement following the **Loop** statement. When used within nested **Do...Loop** statements, **Exit Do** transfers control to the loop that is one nested level above the loop where **Exit Do** occurs.
  
- **Exit For**  
  Provides a way to exit a **For** loop. It can be used only in a **[For...Next](/tB/Core/For-Next.md)** or **[For Each...Next](/tB/Core/For-Next.md)** loop. **Exit For** transfers control to the statement following the **Next** statement. 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.
  
- **Exit While**  
  Provides a way to exit a **[While...Wend](/tB/Core/While-Wend.md)** loop. It can be used only inside a **While...Wend** statement. **Exit While** transfers control to the statement following the **Wend** statement. When used within nested **While...Wend** statements, **Exit While** transfers control to the loop that is one nested level above the loop where **Exit While** occurs. **Exit While** is a twinBASIC extension; classic VBA has no early-exit form for **While...Wend**.
  
- **Exit Function**  
  Immediately exits the **[Function](/tB/Core/Function.md)** procedure in which it appears. Execution continues with the statement following the statement that called the **Function**.
  
- **Exit Property**  
  Immediately exits the **[Property](/tB/Core/Property.md)** procedure in which it appears. Execution continues with the statement following the statement that called the **Property** procedure.
  
- **Exit Sub**  
  Immediately exits the **[Sub](/tB/Core/Sub.md)** procedure in which it appears. Execution continues with the statement following the statement that called the **Sub** procedure.

Do not confuse **Exit** statements with **End** statements. **Exit** does not define the end of a structure.

### Example

This example uses the **Exit** statement to exit a **For...Next** loop, a **Do...Loop**, and a **Sub** procedure.

```tb check_build
Sub ExitStatementDemo() 
  Dim I%, MyNum% 
  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
```