Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples

What is a switch?

The switch statement evaluates a variable once and branches execution to the matching case label, simplifying multi‑way decision making.

Think of it as a clean, efficient if…else if… ladder that selects code blocks based on the variable’s value.

In this C++ tutorial you’ll learn:

When to use a switch?

A switch is ideal when a single variable must be compared against a fixed set of constants. It produces clearer, faster code than a long if…else if… chain.

The break keyword

Inside a switch, break terminates the current case, preventing fall‑through to subsequent cases. If omitted, execution continues into the next case.

When the compiler encounters break, control jumps to the statement following the closing brace of the switch.

Syntax

Typical structure:

switch (variable)
{
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code
}

Example 1

#include <iostream> 
using namespace std;
int main()
{
    int x = 20;
    switch (x)
    {
        case 10: 
            cout << "X is 10"; break;
        case 20: 
            cout << "X is 20"; break;
        case 30: 
            cout << "X is 30"; break;
        default: 
            cout << "X is not 10, 20 or 30"; break;
    }
    return 0;
}

Output:

Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples

Screenshot of the code:

Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples

Code explanation:

  1. Include <iostream> to enable console I/O.
  2. Use the std namespace for brevity.
  3. Define main() as the entry point.
  4. Declare int x = 20;.
  5. Apply switch(x) to compare x against constants.
  6. Case 10: print “X is 10”.
  7. Case 20: print “X is 20”.
  8. Case 30: print “X is 30”.
  9. Default: handle values other than 10, 20, 30.
  10. Each case ends with break; to avoid fall‑through.
  11. Return 0 to indicate successful execution.

Example 2

#include <iostream>  
using namespace std;
int main() {
    int choice;
    cout << "Enter 1, 2 or 3: ";
    cin >> choice;
    switch (choice)
    {
        case 1: 
            cout << "Choice 1"; break;
        case 2: 
            cout << "Choice 2"; break;
        case 3: 
            cout << "Choice 3"; break;
        default: 
            cout << "Not 1, 2 or 3"; break;
    }
}

Output:

Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples

Screenshot of the code:

Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples

Code explanation:

  1. Include <iostream> for I/O.
  2. Use std namespace.
  3. Declare int choice;.
  4. Prompt user: “Enter 1, 2 or 3: ”.
  5. Read input into choice.
  6. Switch on choice to branch to matching case.
  7. Case 1 prints “Choice 1”.
  8. Case 2 prints “Choice 2”.
  9. Case 3 prints “Choice 3”.
  10. Default handles any other input.
  11. Each case ends with break;.

Summary

C Language

  1. Mastering the C++ break Statement
  2. Mastering the C++ Switch‑Case Statement: Syntax, Workflow, and Practical Examples
  3. Mastering the Java Switch Statement: Syntax, Usage, and Best Practices
  4. C++ For Loops Explained: Syntax, Workflow, and Practical Examples
  5. Mastering std::stack in C++: A Comprehensive Guide with Practical Examples
  6. C++ Structs Explained with a Practical Example
  7. C++ Classes & Objects: A Practical Guide with Code Examples
  8. Mastering std::list in C++: Syntax, Functions & Practical Examples
  9. Understanding Switch‑Case in C: Syntax, Examples, and Best Practices
  10. Java Switch‑Case Statement Explained: Syntax, Examples, and Best Practices