In the previous lesson, we have learned how to control the program flow using the If...ElseIf control structure. In this chapter, you will learn another way to control the program flow, that is, the Select Case control structure. However, the Select Case control structure is slightly different from the If....ElseIf control structure . The difference is that the Select Case control structure basically only make decision on one expression or dimension (for example the examination grade) while the If ...ElseIf statement control structure may evaluate only one expression, each If....ElseIf statement may also compute entirely different dimensions. Select Case is preferred when there exist many different conditions because using If...Then..ElseIf statements might become too messy.
|
The format of the Select Case control structure is show below: Select Case expression Case
value1 End Select * The data type specified in expression must match that of Case values.
|
Example 8.1 ' Examination Grades Dim grade As String Private Sub Compute_Click( ) grade=txtgrade.Text Select Case grade Case "A"
|
|
|
|
Example 8.2
Dim mark As Single mark = mrk.Text Case Is >= 85 Case Is >= 70
Case Is >= 60 comment.Caption = "Above Average"
Case Is >= 50 comment.Caption = "Average"
Case Else comment.Caption = "Need to work harder" End Select
End Sub
|
Example 8.3
Example 8.2 could be rewritten as follows:
Dim mark As Single
mark = mrk.Text Case 0 to 49 Case 50 to 59 Case 60 to 69 comment.Caption = "Above Average"
Case 70 to 84 comment.Caption = "Good"
Case Else comment.Caption = "Excellence" End Select
End Sub
|