Saturday, May 14, 2011

Control structures - 1.Branching - 2. Case


Select Case - Another Way to Branch



     In addition to If/Then/Else type statements, the Select Case format can be used when there are multiple selection possibilities.

Select Case marks
Case 0 to 39
            grade="D"
Case 39 to 40
            Grade="C"
Case 41 to 50
            grade="C"
Case 51 to 60
            Grade="B"
Case Else
            Grade="A"
End Select


·         Say we've written this code using the If statement:
     
            If Age = 5 Then
              Category = "Five Year Old"
            ElseIf Age >= 13 and Age <= 19 Then
              Category = "Teenager"
            ElseIf (Age >= 20 and Age <= 35) Or Age = 50 Or (Age >= 60 and Age <= 65) Then
              Category = "Special Adult"
            ElseIf Age > 65 Then
              Category = "Senior Citizen"
            Else
              Category = "Everyone Else"
            End If
     
The corresponding code with Select Case would be:
     
           
Select Case Age
              Case 5
                Category = "Five Year Old"
              Case 13 To 19
                Category = "Teenager"
              Case 20 To 35, 50, 60 To 65
                Category = "Special Adult"
              Case Is > 65
                Category = "Senior Citizen"
              Case Else
                Category = "Everyone Else"
            End Select
     
Notice there are several formats for the Case statement.  Consult on-line help for discussions of these formats.






Private Sub Command1_Click()
Select Case List1.ListIndex
Case 0
            MsgBox ("Fiest name")
Case 1
            MsgBox ("second name")
Case 2
            MsgBox ("Thired name")
Case 3
            MsgBox ("fourth name")
End Select
End Sub
.


No comments:

Post a Comment