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
The keyword for a procedure: Function, Method, Property, or Sub.
Alternatively, one or more of the keywords for loops: Do, For, or While. You write a comma between each keyword to make a list.
identifier
An identifier that is the same as one that you gave in the Begin part of a Begin...When block.
Exit Program
Immediately stops the Program module in which it is used. Program flow continues with the process that started the program.
exit_code
Optional integer value. The default value is zero (0).

Instructions

Exit Do immediately stops the Do...Loop loop in which it is used. Program flow continues with the line after Loop.

Exit For immediately stops the For or For Each loop in which it is used. Program flow continues with the line after End For.

Exit Function immediately stops the Function procedure in which it is used. Program flow goes back 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 the Method procedure in which it is used. Program flow goes back 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 the Property procedure in which it is used. Program flow goes back 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 the Sub procedure in which it is used. Program flow continues on the line after the line that called the procedure.

Exit While immediately stops the While loop in which it is used. Program flow continues with the line after End While.

With Begin...When

You must be careful when you use Exit in a Begin...When block. This construct lets you catch when a loop completes correctly. When you use Exit with keyword_list to stop a loop, then When %Done and When Else are ignored.

You use “Exit identifier” to stop a loop and send execution to the When part with the given identifier.

With nested structures

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

Examples

Begin...When

For Each name$ In names$()
  Begin 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 both loops.
        If array(i, j) = 0 Then Exit For, For
    End
End

See also