Scripting > mrScriptBasic overview > mrScriptBasic language reference > Statements > Select Case
 
Select Case
Select Case conditionally executes a group of statements, depending on the value of an expression.
Syntax
Select Case testexpression
[Case expression-n
[statements-n]] ...
[Case Else
[elsestatements]]
End Select
Arguments
testexpression
Any expression that can be evaluated to return a result. The result type of the expression will be used for comparison with case expressions.
expression-n
An expression that will be evaluated and compared with the test expression. This can contain more than one value, a range of values, or a combination of values and comparison operators.
statements-n
One or more statements executed if testexpression matches any part of expression-n.
elsestatements
One or more statements executed if no previous expression-n has evaluated to be equal to the test expression.
Remarks
If testexpression matches any Case expression-n expression, the statements following that Case clause are executed up to the next Case clause, or for the last clause, up to End Select. Control then passes to the statement following End Select. If testexpression matches an expression-n expression in more than one Case clause, only the statements following the first match are executed.
The Case Else clause is used to indicate the elsestatements to be executed if no match is found between the testexpression and an expression-n in any of the other Case selections. Although not required, it is a good idea to have a Case Else statement in each Select Case block to handle unforeseen testexpression values. If no Case expression-n matches testexpression and there is no Case Else statement, execution continues at the statement following End Select.
Select Case statements can be nested. Each nested Select Case statement must have a matching End Select statement.
Examples
Simple example
This example uses a Select Case statement when cleaning data in a DataManagementScript (DMS) file. The Select Case statement is based on the response given to a categorical question, called Interview, which determined the subsequent questions asked during the interview. Respondents who gave different replies to this question therefore require different cleaning procedures.
Select Case Interview
Case {entering}
EnteringCheck()
Case {leaving}
LeavingCheck()
Case Else
Job.DropCurrentCase()
End Select
Advanced example
This is another example of using a Select Case statement in a DMS file. This time it is used to assign the TotalVisits2 categorical variable a value according to the value of the response in the visits variable. Using a Select Case statement in this situation avoids the use of multiple If...Then...Else statements.
Select Case visits
Case 0
TotalVisits2 = {Band1}
Case 1 To 5
TotalVisits2 = {Band2}
Case 6 To 10
TotalVisits2 = {Band3}
Case 11 To 20
TotalVisits2 = {Band4}
Case 21 To 40
TotalVisits2 = {Band5}
Case 41 To 70
TotalVisits2 = {Band6}
Case > 70
TotalVisits2 = {Band7}
End Select
In a DMS file, you can also test for the question's minimum and maximum value. For example:
Case 21 To visits.Validation.MaxValue
You can use multiple expressions or ranges in each Case clause. For example, the following tests for nonconsecutive values:
Case 1 To 3, > 8
See Example 1: Creating a categorical variable from a numeric variable for more information.
See also
Statements