From Xojo Documentation
|You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!|
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 Each row As List
For i As Integer = 0 To ListBox1.LastRowIndex
If ListBox1.CellTextAt(i, 0) = searchString Then
ListBox1.SelectedRowIndex(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.
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