Exit When Clause

Run a different block of code given what the loop does.

Syntax

start_clause
Exit When [ label_list ]
    [ loop_statements ]
When { Begin | Each | Else | label | DONE | NONE }
    [ when_statements ]
…
end_clause

Parts

start_clause
Mandatory start clause for one of the loop constructs: Do, For, For Each, or While.
Exit When
Mandatory keywords.
label_list
Optional one or more identifiers with a comma between each. You must use each label given here in a subsequent block that starts with When.
loop_statements
Optional one or more Statements that run for Each iteration of the loop. It is equivalent to one of the Clauses When Each or When Else. But you cannot use this block if there is also one of the clauses When Begin, When Each, or When Else.
When
Mandatory keyword you can use again and again.
Begin
Starts a block of statements that runs only one time. It runs after the loop initializes and before When Each or When Else.
Each or Else
Starts a block of statements that runs for each iteration of the loop. Each and Else are equivalent.
label
Starts a block of statements that runs because of «Exit label». Mandatory for each label in label_list. But if no such list is supplied, the compiler does not do this check.
DONE
Starts a block of statements that runs after all iterations completed.
NONE
Starts a block of statements that runs when there were no iterations.
when_statements
Optional statements.
end_clause
Mandatory end clause for its related start clause (start_clause).

Instructions

When label

If you use «When label», the statements in this block of code run because of «Exit label». Control then moves out of the loop.

You can supply a list of labels after the clause Exit When. If you do, you must also supply the related blocks with When and one of these labels.

When DONE

If you use «When DONE», the statements in this block of code run only after the loop stopped as usual. Control then moves out of the loop.

If a loop stopped because of Exit or Return, When DONE does not run. Thus the operation continues as usual for the given statement.

When NONE

If you use «When NONE», the statements in this block of code run only when the body of the loop did not run. Control then moves out of the loop.

A table that summarizes the conditions in which this can occur follows.

Loop constructLoop body not run
While C
When condition C is initially false
Do While C
When condition C is initially false
Do Until C
When condition C is initially true
For I = A To B [ Step D ]
When A > B for a positive D. Or A < B for a negative D
For Each I In G
When group G is #Null or is empty

Note: You cannot use When NONE in Do…Loop While and Do…Loop Until because they run their statements a minimum of one time.

When Each or When Else

If you use When Each or When Else, this block of code becomes the body of the loop. Statements in the block that starts with When Begin run only one time before the initial iteration of the loop.

When Each and When Else are equivalent. If you use one, you cannot use the other in the same loop. We recommend that you select one and use only that one. You can specify one with the directive @Option.

Scope

The scope of variables in these blocks is the same as variables in the usual loop construct. Thus you cannot use variables declared in them after the end of the loop.

Changes in syntax with @Option

There are two areas where you can change the syntax of the statement:

See @Option Directive for more information.

Examples

When label

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

See also

External link