Switch Statement in C++ - Learn C++ - C++ Tutorial - C++ programming
Learn c++ - c++ tutorial - switch statement in c++ - c++ examples - c++ programs
What is Switch Statement in C++
- A switch statement allows a variable to be tested for equality against a list of values.
- Each value is called a case, and the variable being switched on is checked for each switch case.
- It is the chain many if-else statements together, this is difficult to read. Consider the following program:
learn c++ tutorials - switch case statement Example
- Because doing if-else chains on a single variable testing for equality is so common
- C++ provides an alternative conditional branching operator called a switch.
- Here is the same program as above in switch form:
- The overall idea behind switch statements is simple: the switch expression is evaluated to produce a value, and each case label is tested against this value for equality.
- If a case label matches, the statements after the case label are executed.
- If no case label matches the switch expression, the statements after the default label are executed (if it exists).
- Because of the way they are implemented, switch statements are typically more efficient than if-else chains.
- Let’s examine each of these concepts in more detail.
Starting a switch
- We start a switch statement by using the switch keyword, followed by the expression that we would like to evaluate.
- Floating point variables and other non-integral types may not be used here.
- Following the switch expression, we declare a block. Inside the block, we use labels to define all of the values we want to test for equality. There are two kinds of labels.
- The first kind of label is the case label, which is declared using the case keyword and followed by a constant expression.
- A constant expression is one that evaluates to a constant value -- in other words, either a literal (such as 5), an enum (such as COLOR_RED), or a constant variable (such as x, when x has been defined as a const int).
- The constant expression following the case label is tested for equality against the expression following the switch keyword. If they match, the code under the case label is executed.
- It is worth noting that all case label expressions must evaluate to a unique value. That is, you can not do this:
- It is possible to have multiple case labels refer to the same statements. The following function uses multiple cases to test if the ‘c’ parameter is an ASCII digit.
- In the case where c is an ASCII digit, the first statement after the matching case statement is executed, which is “return true”.
Learn C++ , C++ Tutorial , C++ programming - C++ Language -Cplusplus
The default label
- The second kind of label is the default label (often called the “default case”), which is declared using the default keyword. The code under this label gets executed if none of the cases match the switch expression.
- The default label is optional, and there can only be one default label per switch statement. It is also typically declared as the last label in the switch block, though this is not strictly necessary.
- In the isDigit() example above, if c is not an ASCII digit, the default case executes and returns false.
Switch execution and fall-through
- One of the trickiest things about case statements is the way in which execution proceeds when a case is matched.
- When a case is matched (or the default is executed), execution begins at the first statement following that label and continues until one of the following termination conditions is true:
- The end of the switch block is reached
- A return statement occurs
- A goto statement occurs
- A break statement occurs
- Note that if none of these termination conditions are met, cases will overflow into subsequent cases! Consider the following snippet:
- This is probably not what we wanted! When execution flows from one case into another case, this is called fall-through.
- Fall-through is almost never desired by the programmer, so in the rare case where it is, it is common practice to leave a comment stating that the fall-through is intentional.
- A break statement (declared using the break keyword) tells the compiler that we are done with this switch (or while, do while, or for loop).
- After a break statement is encountered, execution continues with the statement after the end of the switch block.
- Let’s look at our last example with break statements properly inserted:
Multiple statements inside a switch block
- One other bit of weirdness about switches is that you can have multiple statements underneath each case without defining a new block.
Variable declaration and initialization inside case statements
- You can declare, but not initialize, variables inside the case statements:
- Note that although variable y was defined in case 1, it was used in case 2 as well. All cases are considered part of the same scope, so a declaration in one case can be used in subsequent cases.
- This may seem a bit counter-intuitive, so let’s examine why. When you define a local variable like “int y;”, the variable isn’t created at that point.
- So with that in mind, it’s a little less weird that a variable declared in one case statement can be used in another cases statement, even if the case statement that declares the variable is never executed.
- However, initialization of variables directly underneath a case label is disallowed and will cause a compile error.
- This is because initializing a variable does require execution, and the case statement containing the initialization may not be executed!
- If a case needs to define and/or initialize a new variable, best practice is to do so inside a block underneath the case statement: