Use the SWITCH...ENDSW command in a program to define a block of possible actions to take depending on the value of an expression.
Options
BREAK
Ends SWITCH processing; use this option within each CASE comparison and in the DEFAULT block.
case-block
Contains one or more commands to execute if the CASE value matches the SWITCH expression.
CASE value
Compares the SWITCH value to another value. If the values match, the commands following CASE are executed; otherwise, the next CASE comparison is checked.
DEFAULT
Provides commands to execute if no CASE comparisons are true.
default-block
Contains one or more commands to execute if no CASE comparisons are true.
(expression)
Determines a value using a text or arithmetic formula. The expression can include other columns from the table, constant values, functions, or system variables such as #date, #time, and #pi.
About the SWITCH...ENDSW Command Structure
SWITCH...ENDSW structures are composed of a series of options. The syntax diagram above shows the entire SWITCH...ENDSW structure, including the SWITCH value, CASE blocks, and the DEFAULT block.
The SWITCH Expression
SWITCH defines the expression to be compared. You can have multiple comparisons, so ENDSW defines the end of the comparisons. The SWITCH expression result must be either an INTEGER or a TEXT data type. The SWITCH expression can be a calculation, constant value, or variable. Any length of text can be compared, but only the first 30 characters are checked in each CASE block.
CASE Blocks
A CASE block consists of three parts: the CASE comparison, the commands following each comparison, and the BREAK statement.
CASE comparisons must be the same data type as the SWITCH expression result - either INTEGER or TEXT. A CASE value cannot be an expression, but must be a constant value or a variable. You can have multiple CASE comparisons to run a single set of commands. For an example of how to use multiple comparisons, see "Examples" below.
The commands following a CASE comparison can include any R:BASE command, including a nested SWITCH ENDSW structure. You can nest as many SWITCH ENDSW structures as memory allows.
Use a BREAK statement as the last command in a CASE block to exit from the SWITCH ENDSW structure. The BREAK command stops R:BASE from checking any additional CASE comparisons.
The DEFAULT Block
You can have only one DEFAULT block for each SWITCH ENDSW structure. A DEFAULT block provides a set of commands to run if none of the CASE comparisons is valid; make the DEFAULT block the last statement block in a SWITCH...ENDSW structure. If a CASE block follows a DEFAULT block, R:BASE generates a warning.
Example
The following SWITCH...ENDSW structure uses a date entered in a FILLIN command in the expression. The TDWK function calculates day of the week as text from the date stored in vday.
FILLIN vday USING 'Enter a date: '
SWITCH (TDWK(.vday))
CASE 'Saturday'
CASE 'Sunday'
WRITE 'This is a weekend day.'
SHOW VARIABLE vday
BREAK
DEFAULT
WRITE 'This is a weekday.'
SHOW VARIABLE vday
BREAK
ENDSW
If you entered 12/17/94 when prompted for the date, the first CASE comparison would check whether the day of the week is the word Saturday. Because the word is Saturday, R:BASE would display the message below. The BREAK command prevents R:BASE from processing the rest of the commands in the SWITCH...ENDSW structure.
This is a weekend day.
12/17/94
If the date entered is not Saturday or Sunday - for example, 12/22/94 - the information in the DEFAULT block would display the following.