Executes one of several groups of statements, depending on the value of an expression. This statement is used to control the program flow 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 disordered.
Syntax:
Select Case expression
Case value1 Block of one or more statements
Case value2 Block of one or more Statements
Case value3 . .
Case Else Block of one or more Statements
End Select
Example 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Grade As String
Grade = InputBox("Enter Grade")
Select Case Grade
Case "A"
Label1.Text = "High Distinction"
Case "B"
Label1.Text = "Credit"
Case "C"
Label1.Text = "Pass"
Case Else
Label1.Text = "Fail"
End Select
End Sub
Example 2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mark As Integer
mark = InputBox("Enter the Marks")
Select Case mark
Case Is >= 85
Label1.Text = "Excellence"
Case Is >= 70
Label1.Text = "Good"
Case Is >= 60
Label1.Text = "Above Average"
Case Is >= 50
Label1.Text = "Average"
Case Else
Label1.Text = "Need to work harder"
End Select
End Sub
Example 3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mark As Integer
mark = InputBox("Enter the Marks")
Select Case mark
Case 0 To 49
Label1.Text = "Need to work harder"
Case 50 To 59
Label1.Text = "Average"
Case 60 To 69
Label1.Text = "Above Average"
Case 70 To 84
Label1.Text = "Good"
Case Else
Label1.Text = "Excellence"
End Select
End Sub
End Class