Exit Statement

Immediately moves control out of a loop, procedure, or program.

Syntax

Exit { keyword_list | identifier }
or
Exit Program [ exit_code ]

Parts

keyword_list
One of the keywords for a procedure: Constructor, Function, Method, Property, or Sub.
Or one or more of the keywords for loops: Do, For, or While. You can write a comma between each loop keyword to make a list.
identifier
An identifier that is the same as one given in a block that starts with When. You can use this construct only after Exit When.
Exit Program
Immediately stops the program. Control returns to the process that started the program. You can use this construct only in a module of type Program.
exit_code
Optional integer value. The default value is zero (0).

Instructions

Exit Constructor immediately stops a procedure of type Constructor. Control returns to the line that called the procedure, if there are more procedures to call.

Exit Do immediately stops a loop of type Do…Loop. Control continues with the line after Loop.

Exit For immediately stops a loop of types For or For Each. Control continues with the line after End For.

Exit Function immediately stops a procedure of type Function. Control returns to the line that called the procedure, if there are more procedures to call. The procedure returns the value of the variable with the same name as the procedure.

Exit Method immediately stops a procedure of type Method. Control returns to the line that called the procedure, if there are more procedures to call. The procedure returns the value of the variable with the same name as the procedure.

Exit Property immediately stops a procedure of type Property. Control returns to the line that called the procedure, if there are more procedures to call. The procedure returns the value of the variable with the same name as the procedure.

Exit Sub immediately stops a procedure of type Sub. Control returns to the line that called the procedure, if there are more procedures to call. The procedure returns the value of the variable with the same name as the procedure.

Exit While immediately stops a loop of type While. Control continues with the line after End While.

Exit Program

Exit Program causes the program to stop. Control returns to the operating system or another parent program that started it.

Your program can return an integer value to show failure with exit_code. Two constants are also available:

We recommend that you use these constants for code portability.

Using Exit with Exit When

You must be careful when you use Exit in the block Exit When. This construct lets you do something when a loop completes. When you use Exit with keyword_list to stop a loop, When DONE and When Each or When Else are ignored.

You use Exit identifier to stop a loop and move control to the line When with the same identifier.

With nested structures

When Exit is used in nested structures of the same type (for example, two For loops), Exit moves out of the most inner structure and gives control to the next higher level. If you must move out of more than one level, you can write each structure's keyword in sequence, with a comma between each one. See the example below.

Examples

Exit When

For Each name$ In names$()
Exit When found
    If name$ = match$ Then Exit found
When found
    PrintLine "Match found."
When DONE
    PrintLine "No matches found."
End For

Nested For loops

For i = 0 To 9
    For j = 0 To 9
        ' If element is zero, exit the two loops.
        If array(i, j) = 0 Then Exit For, For
    End For
End For

See also