Home About Contact Privacy Terms Disclaimer
TECHAntena
Last Updated On: December 28, 2016
Editor: Adeeb C

Select Case Statement in Visual Basic .Net

Category: Programing,VB.NET
Tags

Visual Basic supports the Select Case statement. Select Case statement is same as the Switch Case statement in C and C++. Select Case statement test a variable against a set of values. Where each value is called case, if the variable become equal to one of the cases, then the statement corresponding to the case will be executed.

Syntax

Select [ Case ] expression
 [ Case expressionlist
 [ statements ] ]
 [ Case Else
 [ elsestatements ] ]
End Select

Example

Select day
 Case 1
   Console.WriteLine("Sunday")
 Case 2
   Console.WriteLine("Monday")
 Case 3
   Console.WriteLine("Tuesday")
 Case 4
   Console.WriteLine("Wednesday")
 Case 5
   Console.WriteLine("Thursday")
 Case 6
   Console.WriteLine("Friday")
 Case 7
   Console.WriteLine("Saturday")
 Case Else
   Console.WriteLine("Invalid")
 End Select

Example Program

Module Module1

   Sub Main()
     Dim day As Integer
     Console.WriteLine("Enter the day number:")
     day = Console.ReadLine()
     Select Case day
       Case 1
          Console.WriteLine("Sunday")
       Case 2
          Console.WriteLine("Monday")
       Case 3
          Console.WriteLine("Tuesday")
       Case 4
          Console.WriteLine("Wednesday")
       Case 5
          Console.WriteLine("Thursday")
       Case 6
          Console.WriteLine("Friday")
       Case 7
          Console.WriteLine("Saturday")
       Case Else
          Console.WriteLine("Invalid Number")
     End Select
 End Sub

End Module

If the user enter 1 as the value of the variable day in above program, the output will be Sunday.