In this C++ tutorial, you will learn how the switch statement selects one code block from several alternatives based on the value of an expression. The tutorial covers switch syntax, case labels, break, default, fall-through behavior, flow diagrams, multiple case labels, supported value types, and practical C++ switch examples.

How the C++ Switch Statement Selects a Case

A C++ switch statement evaluates an expression once and compares the resulting value with the values specified by its case labels. When a matching case is found, execution starts from that case.

A switch is useful when one value must be compared against several fixed choices. For example, you can map a numeric menu choice to an action, convert a weekday number to a weekday name, or handle several character commands.

C++ Switch Statement Syntax

Following is the syntax of switch statement in C++.

</>
Copy
switch (expression) {
  case value1:
    // code block
    break;
  case value2:
    // code block
    break;
  default:
    // code block
}

The main parts of this syntax are:

  • expression is evaluated once when the switch statement begins.
  • Each case specifies a constant value to compare with the switch value.
  • break normally ends the switch after a matching case has been handled.
  • default provides an optional fallback when no case value matches.

Execution Steps of a C++ Switch Statement

  1. Start of switch statement. The expression is evaluated to a value.
  2. This value is then compared to each case value.
  3. If it finds a match, corresponding block is executed.
  4. break statement at the end of case block is optional. After executing a block, execution comes out of the loop because of break. If no break statement is given, all the case blocks and default block, next to this block, are executed.
  5. default block is optional. If expression value does not match any of the case values, default block is executed.

One correction to keep in mind when reading the steps above: switch is a selection statement, not a loop. A break statement exits the enclosing switch statement; it does not end a loop unless that break is inside a loop.

If a matching case does not contain break, execution continues into the following case statements. This behavior is called fall-through. It can be intentional, but it should be used carefully because an omitted break can also be a logic error.

C++ Switch Flowchart with break

Following is the execution flow diagram of switch statement with break statement for each of the case blocks.

C++ Switch Statement

C++ Switch Flowchart without break

Following is the execution flow diagram of switch statement without break statement for case blocks.

C++ Switch Statement without Break

Rules for case Values in a C++ Switch

In ordinary C++ switch usage, the switch value is an integral or enumeration value, such as an int, char, or enum. Each case label must use a constant expression whose value can be compared with the switch value.

  • Case values must be known as constant expressions; a changing runtime variable cannot be used as a case label.
  • Two case labels in the same switch cannot represent the same value after conversion to the switch type.
  • The default label is optional.
  • The default label does not have to appear last, although placing it last is common and often easier to read.
  • A break is not required after every case, but omitting it allows execution to continue into the next case.

C++ Switch Statement Examples

1. Select a Weekday with a C++ Switch

Following is an example of Switch statement in C++. In this example, we shall print if the name of weekday based on number.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int day = 25;
   switch (day%7) {
      case 1: {
         cout << "Monday" << endl;
         break;
      }
      case 2: {
         cout << "Tuesday" << endl;
         break;
      }
      case 3: {
         cout << "Wednesday" << endl;
         break;
      }
      case 4: {
         cout << "Thursday" << endl;
         break;
      }
      case 5: {
         cout << "Friday" << endl;
         break;
      }
      default: {
         cout << "Off day" << endl;
      }
   }
}

Output

Thursday

Here, 25 % 7 evaluates to 4. Therefore, execution begins at case 4, prints Thursday, and the following break ends the switch.

2. C++ Switch without break and Fall-Through

Following is an example of Switch statement in C++. We have not used break statement after end of case blocks. In this example, we shall print if the name of weekday based on number.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int day = 24;
   switch (day%7) {
      case 1: {
         cout << "Monday" << endl;
      }
      case 2: {
         cout << "Tuesday" << endl;
      }
      case 3: {
         cout << "Wednesday" << endl;
      }
      case 4: {
         cout << "Thursday" << endl;
      }
      case 5: {
         cout << "Friday" << endl;
      }
      default: {
         cout << "Off day" << endl;
      }
   }
}

Output

Wednesday
Thursday
Friday
Off day

In this program, 24 % 7 is 3, so execution starts at case 3. Because there are no break statements, execution falls through the following cases and finally the default block.

3. Group Multiple case Labels in a C++ Switch

Several case labels can share the same statements. This is useful when different values should produce the same result. The example below classifies Saturday and Sunday as weekend days.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int day = 6;

    switch (day) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            cout << "Weekday" << endl;
            break;
        case 6:
        case 7:
            cout << "Weekend" << endl;
            break;
        default:
            cout << "Invalid day" << endl;
    }

    return 0;
}

Output

Weekend

case 6 has no statements of its own, so execution continues directly into the statements associated with case 7. Both values therefore use the same output and the same break.

4. Use char Values in a C++ Switch

A switch is not limited to decimal integer literals. A char is an integral type in C++, so character values can be used for both the switch expression and case labels.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    char operation = '*';

    switch (operation) {
        case '+':
            cout << "Addition" << endl;
            break;
        case '-':
            cout << "Subtraction" << endl;
            break;
        case '*':
            cout << "Multiplication" << endl;
            break;
        case '/':
            cout << "Division" << endl;
            break;
        default:
            cout << "Unknown operation" << endl;
    }

    return 0;
}

Output

Multiplication

Can a C++ Switch Use std::string?

A traditional C++ switch statement cannot directly switch on a std::string. For string choices, use an ifelse if chain, map the string to an integral or enumeration value first, or use another lookup-based design that fits the program.

For example, a direct statement such as switch (command) is not valid when command is a std::string. Character choices such as 'y' and 'n', however, can be handled with a switch because char is an integral type.

C++ Switch vs if-else for Multiple Conditions

Use a switch when one expression is being compared with several discrete constant values. It usually keeps this type of branching compact because the expression appears once and each alternative is represented by a case label.

Use ifelse if when the decision depends on ranges, relational tests, compound Boolean expressions, strings, or unrelated conditions. For example, a test such as score >= 90 is naturally expressed with if; it is not a single constant case value.

Common C++ Switch Statement Mistakes

  • Forgetting break unintentionally: execution continues into subsequent case blocks.
  • Using a runtime variable as a case value: case labels require constant expressions.
  • Repeating equivalent case values: case values within one switch must be distinct.
  • Expecting switch to test ranges: a case label represents a specific constant value, not a condition such as x > 10.
  • Trying to switch directly on std::string: use string comparisons or convert the input to a suitable integral or enumeration representation.
  • Assuming default is mandatory: it is optional, though it is useful when unmatched values need explicit handling.

C++ Switch Statement Summary

A C++ switch statement evaluates one expression and transfers control to a matching case. A break normally prevents fall-through into later cases, while default handles values that do not match any case. Multiple case labels can share one code block, and integral or enumeration values are the usual inputs for switch-based selection.

In this C++ Tutorial, we learned about C++ Switch statement, consequences of including and not including break statement after each case block, and also about default block.