Select Case Statement in Visual Basic 2010

Select Case Statement in Visual Basic 2010

In the previous article we discussed about Decision Making Structures in Visual basic 2010. HERE Select Case is the second category of structures used for decision making in Visual Basic 2010.

As we have discussed before; when a programmer wants to check various different conditions and based upon those conditions, generate an output he can use the “If … ElseIf” structure. We can do the same thing in an easier manner, too.

Select Case Statement:

Select Case

The select case statement is used to test the value of a variable against a list of case values and when a match is found, a statement of group of statements associated with that case be executed. It is simply when you would like to check different conditions for a variable and based on the output, execute a set of statements.

The general syntax for Select Case is:

It is also possible to use the select case structure in different formats. The syntax explained here is a generalized one.

The value of the expression can be an integer or a string. In case the value of the expression matches any case value, the statement(s) associated with that case value is executed. In case any of the case values do not match, then the else case statement(s) are executed.

Check Also: Looping Structures in Visual Basic . NET (2010)

Example Code: (Format 1)

In this example the case expression value is only a string. The same format can be used when you have an integer or character value as the case expression. ToLower() function is used to change the day value to lower case in order to match the case values.

Output:

1 select case

 

2 select case

 

 

 

 

 

 

 

Check Also: Decision Making Structures in Visual Basic . NET (2010)

Example Code: (Format 2)

In this example, the case value is making use of the keyword (To) which specifies the range between two numbers.

Output:

3 select case

 

 

4 select case

 

 

 

 

 

 

Example Code: (Format 3)

In this example, relational operator is used in the case value expression. Note that, if the first case does not match the case value expression then the second case is tested. So the second condition will be “Salary >= 5000 AND Salary < 8000”. The same rule is applied for the rest of the cases.

Output:

5 select case

 

6 select case

 

 

 

 

 

 

 

Feel free to ask your questions, post your comments and question us further tutorials and articles.

Previous Recursion in C++
Next Top 10 Popular Programming Languages to Learn in 2017

About author

Bahonar Javid
Bahonar Javid 43 posts

<p>* Computer Application Student<br /> * Co-Founder of www.codejow.com<br /> * Love to code and Learn new languages<br /> * Enjoy playing football</p>

View all posts by this author →

You might also like