Exit

From Real Software Documentation

Jump to: navigation, search
Language Keyword

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.


Syntax

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, Real Studio exits whatever loop the Exit statement is in.

If you use Exit with the Do keyword, Real Studio exits the Do loop it is in, even if it is also inside another type of loop.

If you use Exit with the While keyword, Real Studio exits the While loop it is in, even if it is also inside another type of loop.

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.

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 Exit with the Sub or Function keywords, then it will exit the entire method or function. If you use Exit to exit a function, the function will return the default value for the data type of the returned variable.

The Continue statement in a loop continues execution with the next iteration of the loop while skipping over the statements between Continue and the end of the loop.

Examples

This example increments a counter variable. If the user presses Esc on Windows or Linux or Command-Period on Macintosh, the UserCancelled function will return True and the loop will exit. Otherwise, the loop will continue until the Stop variable is True:

Dim counter as Integer
Do
counter=counter+1
If UserCancelled then
Exit
End if
Loop Until Stop

If you use the DoEvents method of the Application class inside a loop such as this, then the UserCancelled function does not detect the Esc key or the Command-period sequence. When you use DoEvents, the user interface remains responsive while the loop is executing, so you can add a button that the user can click to stop the loop.


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.

Function FontAvailable(FontName As String) as Boolean
Dim i,n as Integer
n=FontCount-1
For i=0 to n
If Font(i)=FontName Then
Return True
Exit
End If
Next
Return False

See Also

Continue, Do...Loop, For...Next, GOTO, While...Wend statements.

Personal tools