DO CASE
Topic group Related topics Example
Conditionally processes statements by evaluating one or more conditions and executing the statements following the first condition that evaluates to true.
Syntax
DO CASE
CASE <condition expL 1>
<statements>
[CASE <condition expL 2>
<statements>...]
[OTHERWISE
<statements>]
ENDCASE
CASE <condition expL>
If the condition is true, executes the set of commands between CASE and the next CASE, OTHERWISE, or ENDCASE command, and then transfers control to the line following ENDCASE. If the condition is false, control transfers to the next CASE, OTHERWISE, or ENDCASE command.
<statements>
Zero or more statements to execute if the preceding CASE statement evaluates to true.
OTHERWISE
Executes a set of statements if all the CASE statements evaluate to false.
ENDCASE
A required keyword that marks the end of the DO CASE structure.
Description
DO CASE is similar to IF...ELSE...ENDIF. As with IF conditions, dBASE Plus evaluates DO CASE conditions in the order they're listed in the structure. DO CASE acts on the first true condition in the structure, even if several apply. In situations where you want only the first true instance to be processed, use DO CASE instead of a series of IF commands.
Also, use DO CASE when you want to program a number of exceptions to a condition. The CASE <condition> statements can represent the exceptions, and the OTHERWISE statement the remaining situation.
Starting with the first CASE condition, dBASE Plus does the following.
Evaluates each CASE condition until it encounters one that's true
Executes the statements between the first true CASE statement and the next CASE, OTHERWISE, or ENDCASE (if any)
Exits the DO CASE structure without evaluating subsequent CASE conditions
Moves program control to the first line after the ENDCASE command
If none of the conditions are true, dBASE Plus executes the statements under OTHERWISE if it's included. If no OTHERWISE statement exists, dBASE Plus exits the structure without executing any statements and transfers program control to the first line after the ENDCASE command.
DO CASE is functionally identical to an IF...ELSEIF...ENDIF structure. Both specify a series of conditions and an optional fallback (OTHERWISE and ELSE) if none of the conditions are true. Common style dictates the use of DO CASE when the conditions are dependent on the same variable, for example what key was pressed, while IF...ELSEIF...ENDIF is used when the conditions are not directly related. In addition, DO CASE usually involves more indenting of code.