- Introduction
- Control Structures
- If/Then Selection Structure
- If/Then/Else Selection Structure
- While Repetition Structure
- DoWhile/Loop Repetition Structure
- DoUntil/Loop Repetition Structure
- Do/LoopWhile Repetition Structure
- Do/LoopUntil Repetition Structure
- Assignment Operators
- For/Next Repetition Structure
- Example: Using the For/Next Structure to Compute Compound Interest
- SelectCase Multiple-Selection Structure
- Using the Exit Keyword in a Repetition Structure
- Logical Operators
- Introduction to Windows Application Programming
- Summary
3.13 SelectCase Multiple-Selection Structure
Occasionally, an algorithm contains a series of decisions in which the algorithm tests a variable or expression separately for each value that the variable or expression might assume. The algorithm then takes different actions based on those values. Visual Basic provides the SelectCasemultiple-selection structure to handle such decision making. The program in Fig. 3.8 uses a SelectCase structure to count the number of different letter grades on an exam. Assume that the exam is graded as follows: 90 and above is an "A," 8089 is a "B," 7079 is a "C," 6069 is a "D" and 059 is an "F." The instructor generously gives a minimum grade of 10 for students who were present for the exam. Students not present for the exam receive a 0.
Fig. 3.8 SelectCase structure used to count grades.
Line 7 in Fig. 3.8 declares variable grade as type Integer. This variable stores each grade that is input. Lines 812 declare variables that store the total number grades of each type. Lines 1857 use a While loop to control repetition.
Line 20,
Select Case grade
begins the Select Case structure. The expression following the keywords Select Case is called the controlling expression. The controlling expression (i.e., the value of grade) is compared sequentially with each Case. If a matching Case is found, the code in the Case executes, and program control proceeds to the first statement after the SelectCase structure (line 55).
Common Programming Error 3.1
Duplicate Case statements are logic errors. At execution time, the first matching Case statement is executed.
The first Case statement (line 22) determines whether the value of grade is exactly equal to 100. The next Case statement (line 27) determines whether grade is between 90 and 99, inclusive. Keyword To specifies the range. Lines 3144 use this keyword to present a series of similar Cases.
Common Programming Error 3.2
If the value on the left side of the To keyword in a Case statement is larger than the value on the right side, the Case statement is ignored during program execution, potentially causing a logic error.
When multiple values are tested in a Case statement, they are separated by commas. On line 44, either 0 or any value in the range 10 to 59, inclusive, matches to this Case. Line 48 contains the optional Case Else, which is executed when the input does not match the controlling expression in any of the previous Cases. CaseElse commonly is used to check for invalid input. When employed, the CaseElse must be the last Case. The required EndSelect keywords terminate the SelectCase structure.
Common Programming Error 3.3
When using the optional Case Else statement in a Select Case structure, failure to place the CaseElse as the last Case is a syntax error.
Case statements also can use relational operators to determine whether the controlling expression satisfies a condition. For example,
Case Is < 0
uses keyword Is along with the relational operator < to test for values less than 0.
Testing and Debugging Tip 3.1
Provide a Case Elsw in Select Case structures. Cases not handled in a SelectCase is provided. The inclusion of a CaseElse statement facilitates the processing of exceptional conditions. In some situations, no CaseElse processing is needed.