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 executes the subsequent lines of code. 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 numeric expression that can be represented as a 32-bit signed integer. 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 that can optionally include a line label. A compound statement that includes a SWITCH verb can only be followed by a REM comment. There must be one SWEND for each SWITCH.

The section of code following each CASE should end with an EXITTO or a BREAK. If an EXITTO or BREAK verb is not encountered in the section of code that follows the matched CASE expression, execution keeps going, skipping over subsequent CASE statements. It's helpful to think of CASE statements as labels, as opposed to self-contained code blocks. All statements starting with the CASE value that matched the SWITCH expr are executed until an EXITTO, BREAK or SWEND is encountered, including subsequent CASE verbs, which are ignored. This behavior is known as "fallthrough", and can be a source of subtle bugs if not used with care. Because of the danger of introducing subtle errors, fallthrough should usually be avoided. When it is used, a comment should be included to highlight the intentional use of fallthrough.

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