Tuesday, July 5, 2016

Visual Basic 2015 Lesson 14: Dealing with Multiple Choices Using Select Case

In this lesson, you shall learn how to use the Select Case control structure in Visual Basic 2015  . The Select Case control structure also involves decisions making but it slightly different from the If….ElseIf control structure . The If … Then…ElseIf statement control structure evaluates only one expression but  each ElseIf statement  computes different values for the expression. On the other hand, the Select Case control structure evaluates one expression  for multiple values. Select Case is preferred when there exist multiple conditions as using If…Then..ElseIf statements will become too messy.





14.1 The Select Case…End Select Structure
The structure of the Select Case control structure in Visual Basic 2015 is as follows:
Select Case expressionCase value1
Block of one or more Visual Basic 2015 statements
Case value2
Block of one or more Visual Basic 2015 Statements
Case value3
.
.
Case Else
Block of one or more Visual Basic 2015 StatementsEnd Select
14.2 The usage of Select Case is shown in the following examples
Example 14.1: Examination Grades
In this example, the program will display a message associated with the grade entered by the user.
The CodePrivate Sub BtnShow_Click(sender As Object, e As EventArgs) Handles BtnShow.Click
Dim grade As String
grade = TxtGrade.Text
Select Case grade
Case “A”
MsgBox(”High Distinction”)
Case “A-”
MsgBox(”Distinction”)
Case “B”
MsgBox(”Credit”)
Case “C”
MsgBox(”Pass”)
Case Else
MsgBox(”Fail”)End Select
End Sub
The Output
vb2015_fig14.1
 Figure 14.1


vb2015_fig14.2
Figure 14.2

Example 14.2

In this example, you can use the keyword Is together with the comparison operators to evaluate an expression.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click’Examination Marks
Dim mark As Single
mark = mrk.Text
Select Case mark
Case Is >= 85
MsgBox( “Excellence”)
Case Is >= 70
MsgBox( “Good”)
Case Is >= 60
MsgBox( “Above Average”)
Case Is >= 50
MsgBox( “Average”)
Case Else
MsgBox( “Need to work harder”)
End Select
End Sub
Example 14.3
Example 14.2 can be rewritten as follows:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
‘Examination Marks
Dim mark As Single
mark = Textbox1.Text
Select Case mark
Case 0 to 49
MsgBox( “Need to work harder”)
Case 50 to 59
MsgBox( “Average” )
Case 60 to 69
MsgBox( “Above Average”)
Case 70 to 84
MsgBox( “Good”)
Case 85 to 100
MsgBox(“Excellence”)
Case Else
MsgBox( “Wrong entry, please reenter the mark”)
End Select
End Sub
Example 14.4
Grades in high school are usually presented with a single capital letter such as A, B, C, D or E. The grades can be computed as follow:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
‘Examination Marks
Dim mark As Single
mark = TextBox1.Text
Select Case mark
Case 0 To 49
LblGrade.Text = “E”
Case 50 To 59
LblGrade.Text = “D”
Case 60 To 69
LblGrade.Text = “C”
Case 70 To 79
LblGrade.Text = “B”
Case 80 To 100
LblGrade.Text = “A”
Case Else
LblGrade.Text = “Error, please re-enter the mark”
End Select
End Sub
The output:

Figure 14.3

No comments:

Post a Comment