C++ Switch Statement

C++ Switch Statement

The switch statement in C++ is a control structure that allows you to test a variable against multiple values and execute different pieces of code based on the outcome. It's often used as a more readable alternative to a series of if-else if statements when testing the same variable/expression for equality against various values.

Basic Syntax:

switch(expression) { case constant1: // code to execute if expression == constant1 break; case constant2: // code to execute if expression == constant2 break; ... default: // code to execute if none of the above cases are satisfied } 
  • The expression is evaluated once.
  • The resulting value is compared to the values of each case.
  • If there's a match, the block of code associated with that case is executed.
  • The break keyword ends the switch statement. If omitted, the program will continue to the next cases (fall-through behavior).
  • The default case is optional and can be used for handling unexpected values. It doesn't require a break since it's always the last case (even if written somewhere in the middle).

Example:

Let's use the switch statement to create a program that converts numeric day values into their string counterparts:

#include <iostream> int main() { int day = 3; switch(day) { case 1: std::cout << "Monday"; break; case 2: std::cout << "Tuesday"; break; case 3: std::cout << "Wednesday"; break; case 4: std::cout << "Thursday"; break; case 5: std::cout << "Friday"; break; case 6: std::cout << "Saturday"; break; case 7: std::cout << "Sunday"; break; default: std::cout << "Invalid day"; // For values outside the range 1-7 } return 0; } 

This program would print "Wednesday" since day is set to 3.

Fall-through Behavior:

If the break statement is omitted from a case, the program will continue to execute the next case statements until a break is encountered or the switch statement ends. This is called fall-through behavior.

switch(day) { case 6: case 7: std::cout << "Weekend"; break; default: std::cout << "Weekday"; } 

For values 6 (Saturday) and 7 (Sunday), this would print "Weekend".

Notes:

  1. The expression in the switch statement must evaluate to an integral or enumeration type. Floating-point types or strings cannot be used.
  2. Each case label must be unique within a single switch statement.
  3. Fall-through behavior can sometimes be useful, but it can also lead to errors if not intentional. Make sure to always include a break statement unless you specifically want the fall-through behavior.

Conclusion:

The switch statement provides a clear way to test a single variable or expression against multiple values. It improves code readability and organization, especially when there are many cases to handle. However, always remember to handle the potential fall-through behavior to avoid unexpected results.


More Tags

knockout.js smtp fft cucumberjs cookie-httponly upgrade genealogy bash-completion implode moment-timezone

More Programming Guides

Other Guides

More Programming Examples