Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the use of 'select case' with a given range
By Nidhi Last Updated : November 14, 2024
The "select case" with a given range
Here, we will select an option using "select case" from a given range by entering a choice from the user and then print the appropriate message on the console screen.
VB.Net code to demonstrate the use of 'select case' with a given range
The source code to demonstrate the use of "select case" with the given range is given below. The given program is compiled and executed successfully.
'VB.Net program to demonstrate the "select case" with range.
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
Press any key to continue . . .
Explanation:
In the above program, we created a module Module1 that contains a Main() method. In the Main() method, we created a variable choice of Integer type.
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 above code, we read an integer for selection.
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
In the above code, we used "select case", here we defined multiple cases according to given choices to select options from the given range. If we entered the wrong choice, which is not given then the "Case Else" will be executed.
VB.Net Basic Programs »