Visual Basic Select Case Statement

In Visual Basic, Select...Case statement is useful to execute a single case statement from the group of multiple case statements based on the value of defined expression.

 

By using Select...Case statement in Visual Basic, we can replace the functionality of if…else if statement to provide better readability for the code.

Visual Basic Select Case Statement Syntax

Generally, in Visual Basic the Select...Case statement is a collection of multiple case statements and it will execute only one single case statement based on the matching value of the defined expression.

 

Following is the syntax of defining the Select...Case statement in Visual Basic programming language.

 

Select Case variable/expresison

Case value1

// Statements to Execute

Case value2

//Statements to Execute

....

....

Case Else

// Statements to Execute if No Case Matches

End Select

If you observe the above syntax, we defined a Select...Case statement with multiple case statements. Here, the Select statement will evaluate the expression / variable value by matching with Case statement values (value1, value2, etc.). If variable / expression value matches with any of case statement, then the statements inside of the particular case will be executed.

 

In case, if none of the case statements are matches with the defined expression / variable value, then the statements inside of Else block will be executed and it’s more like Else block in if...else statement.

Visual Basic Select Case Statement Flow Chart

Following is the pictorial representation of Select...Case statement process flow in Visual Basic programming language.

 

Visual Basic Select Case Statement Flow Chart Diagram

 

If you observe the above Select...Case statement flow chart diagram, the process flow of Select..Case statement will starts from the Top to Bottom and in the first case, it will check whether the expression value matching or not.

 

In case, the expression value matches, then it will execute the particular Case statement block and exist the Select statement otherwise it will go to the second Case statement and check whether the expression value is matching or not, same way the search will continue till it finds the right Case statement.

 

In case, if all Case statements are failed to match with the defined expression value, then the Else block statements will be executed and Select statement will come to an end.

Visual Basic Select Case Statement Example

Following is the example of using select...case statement in Visual Basic programming language.

 

Module Module1

    Sub Main()

        Dim x As Integer = 20

 

        Select Case x

            Case 10

                Console.WriteLine("x value is 10")

            Case 15

                Console.WriteLine("x value is 15")

            Case 20

                Console.WriteLine("x value is 20")

            Case Else

                Console.WriteLine("Not Known")

        End Select

 

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we defined Select with multiple Case statements and it will execute the matched Case statements with the expression value.

 

When we execute the above Visual Basic program, we will get the result like as shown below.

 

Visual Basic Select Case Statement Example Result

 

If you observe the above result, the Case statement (20) matches with the defined expression value (20) and executed the statements within the respective Case statement.

Visual Basic Nested Select Case Statements

In Visual Basic, using one Select...Case statement within another Select...Case statement is called a nested Select...Case statements.

 

Following is the example of using nested Select...Case statements in Visual Basic programming language.

 

Module Module1

    Sub Main()

        Dim x As Integer = 10, y As Integer = 5

 

        Select Case x

            Case 10

                Console.WriteLine("X Value: 10")

 

                Select Case y

                    Case 5

                        Console.WriteLine("Nested Switch Value: 5")

 

                        Select Case y - 2

                            Case 3

                                Console.WriteLine("Another Nested Switch Value: 3")

                        End Select

                End Select

 

            Case 15

                Console.WriteLine("X Value: 15")

            Case 20

                Console.WriteLine("X Value: 20")

            Case Else

                Console.WriteLine("Not Known")

        End Select

 

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we used Select...Case statements within another Select...Case statements to implement nested Select...Case statements based on our requirements.

 

When we execute the above Visual Basic program, we will get the result like as shown below.

 

Visual Basic Nested Select...Case Statements Example Result

 

If you observe the above result, the nested Select...Case statements has been executed based on our requirements.

Visual Basic Select Case Statement with Enum

In Visual Basic, we can use enum values with Select...Case statements to perform the required operations.

 

Following is the example of using enum values in c# switch case statement.

 

Module Module1

    Sub Main()

        Dim loc As location = location.hyderabad

 

        Select Case loc

            Case location.chennai

                Console.WriteLine("Location: Chennai")

            Case location.guntur

                Console.WriteLine("Location: Guntur")

            Case location.hyderabad

                Console.WriteLine("Location: Hyderabad")

            Case Else

                Console.WriteLine("Not Known")

        End Select

 

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

 

    Public Enum location

        hyderabad

        chennai

        guntur

    End Enum

 

End Module

If you observe the above example, we defined enum values and using those values in Select Case statements to perform the required operations based on our requirements.

 

When we execute the above Visual Basic program, we will get the result like as shown below.

 

C# Switch Case Statement with Enums Example Result

 

If you observe the above result, the Select Case statement which matches with the enum value has been printed in console window.

 

This is how we can use the enums with Select case statements in Visual Basic to perform the operations based on our requirements.