Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

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," 80–89 is a "B," 70–79 is a "C," 60–69 is a "D" and 0–59 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.

Figure 3.8Fig. 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 8–12 declare variables that store the total number grades of each type. Lines 18–57 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 31–44 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.

  • + Share This
  • 🔖 Save To Your Account