Exit
From Xojo Documentation
The Exit statement causes control to exit 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
Exit [For | Do | While]
or
Exit [ For loopVariable]
or
Exit [ Sub | Function ]
Part | Type | Description |
---|---|---|
LoopVariable | Datatype of a loop variable: Integer, Single, or Double. | The loop variable that controls iteration of the For statement that you want to exit. |
Notes
If you use Exit without any optional keywords, whatever loop the Exit statement is in is exited.
If you use Exit with the Do keyword, the Do loop it is in, even if it is also inside another type of loop, is exited.
If you use Exit with the While keyword, the While loop it is in, even if it is also inside another type of loop, is exited.
If you use Exit with the For keyword without passing a parameter, it exits the innermost For loop it is in, even if it is also inside another loop.
If you have nested For statements, you can pass the For keyword the variable that controls the loop you want to exit. For example, if you are testing all the elements of a two-dimensional array with nested For statements, you can use the loop variable for the outermost loop to exit at that point.
If you use Exit with the Sub or Function keywords, then it will exit the entire method or function as if you used Return. Exit Function cannot specify a return value, so if you use it to exit a function, the function will return the default value for the data type of the function.
Sample Code
This example searches a Listbox for a value in the first column, highlighting the first row that is found and exiting the loop:
For Each row As List
For i As Integer = 0 To ListBox1.LastRowIndex
If ListBox1.CellValueAt(i, 0) = searchString Then
ListBox1.SelectedIndex(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 Exit to get out of the loop when it finds the font that it was looking for.
Var fc As Integer
fc = FontCount - 1
Var fontFound As Boolean = False
For i As Integer = 0 To fc
If Font(i) = FontName Then
fontFound = True
Exit
End If
Next
Return fontFound
End Function
See Also
Continue, Do...Loop, For...Next, GOTO, Return, While...Wend statements.