Use the SWITCH...ENDSW command in a program to define a block of possible actions to take depending on the value of an expression. The SWITCH and CASE statements help control complex conditional and branching operations.
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 the 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
The SWITCH statement is a control statement that handles multiple selections by passing control to one of the CASE statements within its body whose value matches the initial expression. The switch statement transfers control to a statement within its body. The syntax diagram shows the entire SWITCH...ENDSW, including the SWITCH value, CASE blocks, and the DEFAULT block.
SWITCH ([expression])
CASE [value]
[case-block]
BREAK
DEFAULT
[default-block]
BREAK
ENDSW
The SWITCH statement allows a developer to control the order in which the code is executed because it is a conditional statement. In addition to handling multiple selections by passing control to one of the CASE statements within its body, the SWITCH provides an efficient mechanism for controlling, tracing, and debugging output at run time using external settings. Practically, you may define a common command file template/Custom Form Action and then use the R:BASE percent variables (%) to pass all required parameters for SWITCH and CASE values. A nested IF...ENDIF can be rewritten as one SWITCH statement.
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
The SWITCH statement can include any number of CASE instances, but no two case statements can have the same value. 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. Otherwise, R:BASE will execute every CASE statement even if only one condition is met.
The DEFAULT Block
Use the DEFAULT block to provide a set of commands to be executed if none of the CASE comparisons matches the expression. You can have only one DEFAULT block for each SWITCH…ENDSW structure. The DEFAULT block should be located in 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 DIALOG command in the expression. The TDWK function calculates day of the week as text from the date stored in vday.
DIALOG 'Enter a date:' vday vendkey 1
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.
This is a weekday.
12/22/94