Home >>VB.Net Programs >VB.Net program to demonstrate the use of 'select case' with a given range

VB.Net program to demonstrate the use of 'select case' with a given range

Write a VB.Net program to demonstrate the use of 'select case' with a given range

Here, by entering a user's choice and then printing the appropriate message on the console screen, we select an option using "select case" from a given range.

Program :

Below is the source code to demonstrate the use of 'select case' for the range given. The program given is compiled and successfully executed.

Example


Module Module1
    Sub Main()
        Dim choice As Integer
        Console.WriteLine("*****************************")
        Console.WriteLine("     (1)   : India")
        Console.WriteLine("     (2-4) : Australia")
        Console.WriteLine("     (5-8) : USA")
        Console.WriteLine("     (9-10): UK")
        Console.WriteLine("*****************************")
        Console.Write("Select your country: ")
        choice = Integer.Parse(Console.ReadLine())
        Select Case choice
            Case 1
                Console.WriteLine("India")
            Case 2 To 4
                Console.WriteLine("Australia")
            Case 5 To 8
                Console.WriteLine("USA")
            Case 9 To 10
                Console.WriteLine("UK")
            Case Else
                Console.WriteLine("Invalid choice")
        End Select
    End Sub
End Module
	
Output:
*************************
(1) : India
(2-4) : Australia
(5-8) : USA
(9-10): UK
*************************
Select your country: 7
USA

Explanation:

We created a Module1 module in the program above that contains the Main() method. We established a variable choice of the Integer type in the Main() method.

Console.WriteLine("*************************")
Console.WriteLine("     (1)   : India")
Console.WriteLine("     (2-4) : Australia")
Console.WriteLine("     (5-8) : USA")
Console.WriteLine("     (9-10): UK")
Console.WriteLine("*************************")
Console.Write("Select your country: ")
choice = Integer.Parse(Console.ReadLine())

In the code above, for selection, we read an integer.

Select Case choice
    Case 1
        Console.WriteLine("India")
    Case 2 To 4
        Console.WriteLine("Australia")
    Case 5 To 8
        Console.WriteLine("USA")
    Case 9 To 10
        Console.WriteLine("UK")
    Case Else
        Console.WriteLine("Invalid choice")
End Select

We used "select case" in the above code, where we specified several cases according to the choices given to select options from the given range. If the wrong choice is entered, and is not given, then the "Case Else" will be executed.


No Sidebar ads