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.
Or 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 line Begin of Begin…When.
Exit Program
Immediately stops the program module. Control returns to the process that started the program.
exit_code
Optional integer value. The default value is zero (0).

Instructions

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

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

Exit Function immediately stops a procedure of type Function. Execution 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. Execution 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. Execution 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. Execution 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. Execution continues with the line after End While.

With Begin…When

You must be careful when you use Exit in Begin…When. 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 move execution to the line When with the given 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, then you can write each structure's keyword in sequence, 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 the two loops.
        If array(i, j) = 0 Then Exit For, For
    End For
End For

Exit Program

TODO

See also