Pages

Saturday, May 7, 2016

VB.Net Chapter 8 - Select Case Control Structure

VB.Net Chapter 8 -  Select Case Control Structure 



In This Chaper
• Understanding The Select Case ….End Select Structure

In the previous Chapter, 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 Select Case ...End Select control structure is shown below:

Select Case test expression  
   Case expression list 1 
        Block of one or more VB statements  
   Case expression list 2 
        Block of one or more VB Statements  
   Case expression list 3 
        Block of one or more VB statements  
   Case expression list 4  
               Block of one or more VB statements  
   Case Else  
        Block of one or more VB Statements  
End Select


Example 8.1

Based on Example 7.4, you can rewrite the code using Select Case...End Select, as shown below.

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 0 to 49  
   Label1.Text = "Need to work harder"  
Case 50 to 59  
   Label2.Text = "Average"  
Case 60 to 69  
   Label3.Text= "Above Average"  
 Case 70 to 84  
   Label4.Text = "Good"  
Case Else  
   Label5.Text= "Excellence"  
End Select  
End Sub


Example 8.2

In this example, you can use the keyword Is together with the comparison operators.

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
     Label1.Text= "Excellence"
Case Is >= 70
     Label2.Text= "Good"
Case Is >= 60
     Label3.Text = "Above Average"
Case Is >= 50
     Label4.Text= "Average"
Case Else
     Label5.Text = "Need to work harder"
End Select
End Sub


Example 8.3 

You also can rewrite Example 8.2 by omitting the keyword IS, as shown here:

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 0 to 49
    Label1.Text = "Need to work harder"
Case 50 to 59
    Label2.Text = "Average"
Case 60 to 69
    Label3.Text= "Above Average"
Case 70 to 84
    Label4.Text = "Good"
Case Else
    Label5.Text= "Excellence"
End Select
End Sub


Summary
In this chapter, you learned how to control program flow using the Select Case control structure. You also learned how to write code for the practical usage of the Select Case control structure, such as the program that processed examination marks. 

0 comments:

Post a Comment