August
1997: Using SWITCH to Make Decisions
Often in your macros, you'll need to make decisions based on user input, the current
WordPerfect state, or other factors. The SWITCH command can be used to test several
possible choices and allow the macro to perform actions based on some value in a
convenient and efficient fashion. The SWITCH command specifies the variable or
expression that you want to test. You'll need other commands to specify what should happen
if the test variable equals a given value. The command you use to specify the possible
value is the CASEOF command. You can also use a DEFAULT command to specify what actions
will happen if none of the CASEOF values equal the test variable. At the end of the entire
SWITCH structure you must use an ENDSWITCH command.
A complete SWITCH command sequence is shown in the example below:
SWITCH(Test)
CASEOF (possible value of Test):
//[commands]
CASEOF(possible value of Test):
//[commands]
CASEOF (possible value of Test):
//[commands]
DEFAULT:
// [optional commands]
ENDSWITCH
SWITCH
The SWITCH command is the first element of the above structure. There is one parameter in
the SWITCH command in which you must specify the variable or expression to be tested.
Usually, this will be the name of a variable.
CASEOF
One CASEOF command should be included for each possible value of the text expression,
where a different set of resulting commands is required. Where an identical set of
resulting commands is required, you can list several different possible values in one
CASEOF command, as shown in the example below. You can specify as many CASEOFs as needed,
and you can include more than one possible value in each CASEOF.
The CASEOF command is case sensitive; a value of CAT is not the same as cat. If the
value might possibly be either in uppercase or lowercase letters, you should list each
possible value in the CASEOF, or use a TOUPPER or TOLOWER command to assure that a match
will be found regardless of the case used when the answer was typed in. The example below
uses TOLOWER to make sure all characters typed by the user are converted to lowercase,
thus increasing the probability of a match with one of the CASEOF values.
DEFAULT
The DEFAULT command is used in a SWITCH statement to give instructions on what to do when
the expression being tested does not match any of the values specified in the CASEOF
statements. Any commands can be included for the default actions. In the example below, if
the user types BIRD, none of the values listed in the CASEOF commands would match the
user's input. The commands in the DEFAULT section would then be invoked.
The DEFAULT command must come after all CASEOF commands. If you place the DEFAULT
before any CASEOF commands, a syntax error will occur when you compile the macro. Note
that a colon (:) must follow the DEFAULT command, as shown in the example above.
ENDSWITCH
The ENDSWITCH command is used to mark the end of a SWITCH statement. A syntax error will
occur if ENDSWITCH is left out. Once the SWITCH test value is matched in a CASEOF and the
appropriate commands invoked, execution skips the rest of the commands in the SWITCH
statement. The command following the ENDSWITCH will be invoked next. Likewise, if the
DEFAULT commands were invoked, or no matches were found, execution moves to the ENDSWITCH.
An Example
In the example below, the SWITCH command tests the variable vPet, which is assigned from
user input in a GETSTRING command. If the input is equal to any of the values listed in
the CASEOF commands, the commands following the matching CASEOF will be invoked. If none
of the values listed in the CASEOF commands equal vPet, the commands listed under DEFAULT:
will be invoked instead.
Once a match with a value is found and the commands in that CASEOF invoked, execution
skips past all the rest of the CASEOFs, and the command following the ENDSWITCH is
invoked.