Purpose
Decides which group of statements to execute, based on the current value of an expression.
Restrictions
You cannot issue a Do Case statement through the MapBasic window.
Syntax
Do Case do_expr
Case case_expr [ , case_expr ]
statement_list
[ Case ... ]
[ Case Else
statement_list ]
End Case
do_expr is an expression.
case_expr is an expression representing a possible value for do_expr.
statement_list is a group of statements to carry out under the appropriate circumstances.
Description
The Do Case statement is similar to the If...Then statement, in that Do Case tests for the existence of certain conditions, and decides which statements to execute (if any) based on the results of the test. MapBasic's Do Case statement is analogous to the BASIC language's Select Case statement. (In MapBasic, the name of the statement was changed to avoid conflicting with the Select statement).
In executing a Do Case statement, MapBasic examines the first Case case_expr clause. If one of the expressions in the Case case_expr clause is equal to the value of the do_expr expression, that case is considered a match. Accordingly, MapBasic executes the statements in that Case's statement_list, and then jumps down to the first statement following the End Case statement.
If none of the expressions in the first Case case_expr clause equal the do_expr expression, MapBasic tries to find a match in the following Case case_expr clause. MapBasic will test each Case case_expr clauses in succession, until one of the cases is a match or until all of the cases are exhausted.
MapBasic will execute at most one statement_list from a Do Case statement. Upon finding a matching Case, MapBasic will execute that Case's statement_list, and then jump immediately down to the first statement following End Case.
If none of the case_expr expressions are equal to the do_expr expression, none of the cases will match, and thus no statement_list will be executed. However, if a Do Case statement includes a Case Else clause, and if none of the Case case_expr clauses match, then MapBasic will carry out the statement list from the Case Else clause
Note that a Do Case statement of this form:
Do Case expr1
Case expr2
statement_list1
Case expr3, expr4
statement_list2
Case Else
statement_list3
End Case
would have the same effect as an If...Then statement of this form:
If expr1 = expr2 Then
statement_list1
ElseIf expr1 = expr3 Or expr1 = expr4 Then
statement_list2
Else
statement_list3
End If
Example
The following example builds a text string such as "First Quarter", "Second Quarter", etc., depending on the current date.
Dim cur_month As Integer, msg As String
cur_month = Month( CurDate() )
Do Case cur_month
Case 1, 2, 3
msg = "First Quarter"
Case 4, 5, 6
msg = "Second Quarter"
Case 7, 8, 9
msg = "Third Quarter"
Case Else
msg = "Fourth Quarter"
End Case
See Also:
If...Then statement