If there are too many else if statements, code can become messy and difficult to follow. In this scenario, a better solution is to use a switch statement. The switch statement simply replaces multiple else ifstatements. The following sample shows how you can use a switch statement to replace a collection of else if clauses.
char response = 'y';
// Block of code executes if the value of response is y.
// Block of code executes if the value of response is Y.
// Block of code executes if the value of response is n.
// Block executes if none of the above conditions are met.
Notice that there is a block labeled default:. This block of code will execute when none of the other blocks match. The default block is optional.
In each case statement, notice the break keyword. This causes control to jump to the end of the switchafter processing the block of code. If you omit the break keyword, the application may not perform as you anticipate. In other languages, such as C#, omitting the break; keyword will cause the code to no longer compile.
Without the break statement, the code will "fall through" to the remaining cases until it encounters a break statement. Be very careful in using fall through logic in your switch statements. The most common use for a fall through scenario is when you want to handle multiple cases with a single statement or set of statements.
If you are coming from another programming language, such as C#, that also uses the switch statement, you might notice that in the C# language, you can use string values in your switch statements and don't have to use integers or enumerated types. C++ switch statements support the following data types as expressions:
intrinsic data types such as int or char
This contents come from Introduction to C++