This lesson will discuss the switch() statement, which is a flow control similar to an if() statement. An apt analogy is that of a switching railroad track that changes course depending on a certain condition being met. The main difference between a switch() and an if() is the former evaluates a single operand against a selection of case scenarios, whereas an if() can have a cascade of varying evaluations each using multiple operands. In both conditional statements, different blocks of code will execute depending on if an evaluation match is found.
For this lesson, create a new ASP.NET project called “CS-ASP_054” with a single resultLabel Control:
You can access the switch() code snippet by typing in “switch” and hitting the tab key twice:
A switch() statement typically consists of:
A single operand to be evaluated.
Conditions (cases) evaluated against that operand.
A default action if none of the conditions evaluate True.
The above example would display “Default (Optional)” because none of the cases, 0 or 1, match the actual value of the operand being evaluated, 4. However, if a case does match the value of the evaluated element you will see the “code block” execute for the particular case:
The break keyword tells the compiler to exit – or break out of – the switch() statement after the code block has completed. If you do not use a break after a case, it groups it with the next case in line. This could be read as saying “if the case is 0 or 1, perform the code block for case 1)
You can also use the return keyword, which would exit the switch() statement entirely, as well as exiting the method it is operating within:
It’s important to realize that returning out of the method happens right away, meaning that any code occurring below that keyword will never execute:
You can use the goto keyword to execute a code block associated with another case. In this scenario, if case 2 is a match it will add “Case 2” to result and then immediately go to case 99 and break out of the switch() entirely:
To make this a bit more obvious, change the following case and run the application:
You can even perform a mathematical calculation and return the result as the matching case. This is equivalent to writing “case 2”:
You can also use the switch() to perform exception handling:
Switch() statements work particularly well with enums, and there are even some handy shortcuts built into Visual Studio to facilitate this. To demonstrate this, let’s re-create the Character class and CharacterType enum from the previous lesson:
In Page_Load() type in the following code, using the code-snippet shortcut for the switch() statement:
After you type in the hero.Type property hit the Enter key on your keyboard twice, and Visual Studio will extrapolate the available values for the enum as cases within the switch():
All you have to do now is add your case specific code blocks, and you have a switch() and enum working in tandem. The general rule of thumb is to use switch() statements when evaluating possible values in enums and you need to check a variety of case scenarios against a single operand, otherwise use a switch() wherever it produces a more elegant solution to what an if() statement would provide.
Lesson 54 - Understanding the switch() Statement