View contents of this chapter

The Select Case Statement

The Select Case statement is suitable for checking multiple conditions. This statement selects from among pieces of code based on the value of an integral expression. Here is the Select Case statement's format:

Select Case testExpression
 Case expressionMatch
      Block of statements
 Case expressionMatch1
      Block of statements
      ....
      ....
 Case expressionMatchN
      Block of statements
End Select

Consider the following program segment:

1: Select Case textGrade.Text
2:  Case "A"
3:      lblShow.Caption = "Great"
4:  Case "B"
5:      lblShow.Caption = "Good"
6:  Case "C"
7:      lblShow.Caption = "Work Hard"
8:  Case Else
9:      lblShow.Caption = "Error"
10: End Select

If the value in the textGrade is "A" , line 3's Case body executes, and then VB skips all the remaining cases. Similarly, If the value in the textGrade is "C" , line 6's Case body executes.

If the user types a grade other than A, B, C, the Case Else is executed.

The Is Keyword

Select Case value
  Case Is >= 10
      'This block will be executed when value is
      'greater than or equal to 10
  Case Is >= 5
      'This block will be executed when value is
      'greater than or equal to 5
End Select

The To Keyword

Select Case A
  Case 1 To 5
      'This block will be executed when A is
      'in range 1 To 5
  Case Else
      'all other value of A
End Select

 



© Universal Teacher Publications        INDEX Previous Screen Next Screen