Begin…When Statements

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

Syntax

start_clause
Begin [ label_list ]
    [ loop_statements ]
When { 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.
Begin
Mandatory keyword.
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 statements that execute a minimum of one time.
If there is no subsequent When Each or When Else, these statements execute again and again. But, if there is such a block, these statements execute only one time at the start of the loop. Then execution moves to When Each or When Else for all subsequent iterations.
When
Mandatory one or more times
Each or Else
Specifies the start of statements that execute for each iteration of the loop. Each and Else are equivalent.
label
Specifies the start of statements that execute 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
Specifies the start of statements that execute after all iterations completed.
NONE
Specifies the start of statements that execute when no iterations executed.
when_statements
Optional executable 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 execute because of «Exit label». Execution then moves out of the loop.

You can supply a list of labels after the keyword Begin. 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 execute only when the loop stops as usual. Execution then moves out of the loop.

If a loop stops because of Exit or Return, When DONE does not execute. Thus execution continues as usual for the given statement.

When NONE

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

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

Loop constructLoop body not executed
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 execute 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 Begin execute 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$()
Begin found
    If name$ = match$ Then Exit found
When found
    PrintLine "Match found."
When DONE
    PrintLine "No matches found."
End For

See also

External link