ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

Here, we are going to demonstrate the use of 'select case' with a given range in VB.Net.
Submitted by Nidhi, on December 03, 2020

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.

Program/Source Code:

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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.