SWITCH .. CASE .. SWEND Verbs

Syntax

SWITCH expr
CASE value;
CASE value;
CASE DEFAULT;
SWEND

For BBj-specific information, see SWITCH .. CASE .. SWEND Verbs - BBj.

Description

The SWITCH verb is a multi-way decision maker that tests if expr matches any value and branches. Compared to multivalue IF statements, SWITCH allows easier (but not faster) processing. ON .. GOTO is faster than SWITCH for handling most constant values.

The expr and value parameters can be any valid expression resulting in an integer (the range for a valid integer expression is 0 to 32767). Expressions are compared for equality in successive lines starting with the first CASE statement. When a match is obtained, execution continues after the CASE expression. If a CASE DEFAULT is found before a matching value, the code following the CASE DEFAULT will be executed.

SWITCH, CASE, and SWEND must be at the start of a line but can optionally follow a line label. Existing programs in which SWITCH appears elsewhere on a line will continue to execute as previously, but attempting to recompile the line will produce an error.

A SWITCH statement can only be followed by REM. There must be one SWEND for each SWITCH. A CASE may be terminated with either an EXITTO or a BREAK. If an EXITTO or BREAK verb is not encountered after the matching CASE, execution continues with the next CASE.

Example

1010 SWITCH CTL
1020 CASE 0
1030 CASE 1; EXITTO 2000
1040 CASE 2; LET FIELD=FIELD-1; BREAK
1050 CASE 3; LET FIELD=1; EXITTO 6000
1060 CASE 4; LET FIELD=1000; EXITTO 6000
1070 CASE DEFAULT; BREAK
1080 SWEND; GOTO 1000

See Also

Verbs - Alphabetical Listing