Keyword

# Exit

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

The <span class="title-ref">Exit</span> statement causes control to <span class="title-ref">Exit</span> a loop and jump to another line of code without the loop conditions being satisfied. The optional keywords enable you to control where execution will resume.

## Usage

``` xojo
Exit [ For | Do | While ]
```

or

``` xojo
Exit [ For loopVariable ]
```

or

``` xojo
Exit [ Sub | Function ]
```

| Part         | Type                                                                                                                                    | Description                                                                                                             |
|--------------|-----------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------|
| loopVariable | Datatype of a loop variable: `Integer</api/data_types/integer>`, `Single</api/data_types/single>`, or `Double</api/data_types/double>`. | The loop variable that controls iteration of the `For</api/language/loops/for...next>` statement that you want to exit. |

## Notes

If you use <span class="title-ref">Exit</span> without any optional keywords, whatever loop the <span class="title-ref">Exit</span> statement is in is exited.

If you use <span class="title-ref">Exit</span> with the `Do</api/language/loops/do...loop>` keyword, the `Do</api/language/loops/do...loop>` loop it is in, even if it is also inside another type of loop, is exited.

If you use <span class="title-ref">Exit</span> with the `While</api/language/loops/while...wend>` keyword, the `While</api/language/loops/while...wend>` loop it is in, even if it is also inside another type of loop, is exited.

If you use <span class="title-ref">Exit</span> with the `For</api/language/loops/for...next>` keyword without passing a parameter, it exits the innermost `For</api/language/loops/for...next>` loop it is in, even if it is also inside another loop.

If you have nested `For</api/language/loops/for...next>` statements, you can pass the `For</api/language/loops/for...next>` keyword the variable that controls the loop you want to <span class="title-ref">Exit</span>. For example, if you are testing all the elements of a two-dimensional array with nested `For</api/language/loops/for...next>` statements, you can use the loop variable for the outermost loop to <span class="title-ref">Exit</span> at that point.

``` xojo
For i = 0 To 255
  For j = 0 To 255
    If myArray(i, j) = 23 Then
      Exit For i
    End If
  Next
Next
```

If you use <span class="title-ref">Exit</span> with the `Sub</api/language/sub>` or `Function</api/language/function>` keywords, then it will <span class="title-ref">Exit</span> the entire method or function as if you used `Return</api/language/return>`. <span class="title-ref">Exit</span> Function cannot specify a return value, so if you use it to <span class="title-ref">Exit</span> a function, the `function</api/language/function>` will return the default value for the data type of the function.

## Sample code

This example searches a `ListBox</api/user_interface/desktop/desktoplistbox>` for a value in the first column, highlighting the first row that is found and exiting the loop:

``` xojo
Var searchString As String = "text to find"

For i As Integer = 0 To ListBox1.LastRowIndex
  If ListBox1.CellTextAt(i, 0) = searchString Then
    ListBox1.SelectedRowIndex(i) = True
    Exit
  End If
Next
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This is an example of a function that determines if the font named passed is installed on the user's computer. It uses <span class="title-ref">Exit</span> to get out of the loop when it finds the font that it was looking for.

``` xojo
Function FontAvailable(FontName As String) As Boolean
  Var fc As Integer
  fc = System.FontCount - 1

  Var fontFound As Boolean = False

  For i As Integer = 0 To fc
    If System.FontAt(i) = FontName Then
      fontFound = True
      Exit
    End If
  Next

  Return fontFound
End Function
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Continue</api/language/loops/continue>`, `Do...Loop</api/language/loops/do...loop>`, `For...Next</api/language/loops/for...next>`, `GoTo</api/language/goto>`, `Return</api/language/return>`, `While...Wend</api/language/loops/while...wend>` statements.

</div>
