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.
|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.|
If you use Exit without any optional keywords, whatever loop the Exit statement is in is exited.
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.
This example searches a Listbox for a value in the first column, highlighting the first row that is found and exiting the loop:
For i As Integer = 0 To ListBox1.ListCount - 1
If ListBox1.Cell(i, 0) = searchString Then
ListBox1.Selected(i) = True
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.
Dim fc As Integer
fc = FontCount - 1
Dim fontFound As Boolean = False
For i As Integer = 0 to fc
If Font(i) = FontName Then
fontFound = True