Group switch Statement in C++

 


In C++, a group switch statement involves handling multiple case values that should result in the same block of code. Instead of writing separate code for each case, you can group multiple case labels to make the code more concise and efficient.


1️⃣ Syntax of Grouped switch Cases

In a grouped switch statement, multiple case labels are written one after the other without a break between them. These cases will execute the same block of code.

switch (expression) {
    case value1:
    case value2:
    case value3:
        // Code to execute if expression matches value1, value2, or value3
        break;
    case value4:
        // Code to execute if expression matches value4
        break;
    default:
        // Code to execute if no case matches
}
  • The group of case labels will fall through to execute the same code without a break.
  • You can group multiple case labels without writing the same code repeatedly.

2️⃣ Example: Group switch with Multiple Days of the Week

#include <iostream>
using namespace std;

int main() {
    int day;
    cout << "Enter a number (1-7) for the day of the week: ";
    cin >> day;

    switch (day) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            cout << "Weekday" << endl;  // These cases group to print "Weekday"
            break;
        case 6:
        case 7:
            cout << "Weekend" << endl;  // These cases group to print "Weekend"
            break;
        default:
            cout << "Invalid day number!" << endl;
    }

    return 0;
}

Output 1 (if input is 3):

Weekday

Output 2 (if input is 7):

Weekend

📌 Explanation:

  • Days 1-5 are grouped to print "Weekday".
  • Days 6-7 are grouped to print "Weekend".
  • If the input is invalid, the default case will handle it.

3️⃣ Example: Grouping Menu Options

#include <iostream>
using namespace std;

int main() {
    int choice;
    cout << "Enter your choice (1-4):\n";
    cout << "1. Start\n";
    cout << "2. Stop\n";
    cout << "3. Pause\n";
    cout << "4. Exit\n";
    cout << "Choice: ";
    cin >> choice;

    switch (choice) {
        case 1:
        case 2:
            cout << "Action in progress..." << endl;
            break;
        case 3:
            cout << "Paused!" << endl;
            break;
        case 4:
            cout << "Exiting program." << endl;
            break;
        default:
            cout << "Invalid choice!" << endl;
    }

    return 0;
}

Output 1 (if input is 1):

Action in progress...

Output 2 (if input is 3):

Paused!

Output 3 (if input is 4):

Exiting program.

4️⃣ Example: Grouping Grades

In a grouped switch for grading systems, we can group similar grades together (e.g., A, B, and C can be considered passing grades).

#include <iostream>
using namespace std;

int main() {
    char grade;
    cout << "Enter your grade (A-D): ";
    cin >> grade;

    switch (grade) {
        case 'A':
        case 'B':
        case 'C':
            cout << "You passed!" << endl;
            break;
        case 'D':
            cout << "You barely passed." << endl;
            break;
        default:
            cout << "Invalid grade!" << endl;
    }

    return 0;
}

Output 1 (if input is B):

You passed!

Output 2 (if input is D):

You barely passed.

5️⃣ Benefits of Using Grouped switch

  • Reduces code duplication: Grouping multiple case values helps avoid repetition of the same code.
  • Improves readability: Instead of having many case blocks with the same code, you can group them into one.
  • Efficient management of conditions: It makes your code cleaner and easier to maintain when you have similar conditions.

6️⃣ Grouping with break Statements

Although grouped cases fall through to the same block of code, you can use the break statement after the grouped block to exit the switch statement.

#include <iostream>
using namespace std;

int main() {
    int num;
    cout << "Enter a number (1-5): ";
    cin >> num;

    switch (num) {
        case 1:
        case 2:
            cout << "Option 1 or 2 selected" << endl;
            break;
        case 3:
        case 4:
            cout << "Option 3 or 4 selected" << endl;
            break;
        case 5:
            cout << "Option 5 selected" << endl;
            break;
        default:
            cout << "Invalid input!" << endl;
    }

    return 0;
}

Output 1 (if input is 2):

Option 1 or 2 selected

Output 2 (if input is 4):

Option 3 or 4 selected

7️⃣ Conclusion

  • The grouped switch statement helps you handle multiple case values with the same block of code.
  • It's a great tool when several values have the same result, helping to make the code more efficient and easier to maintain.
  • Use it when you have many conditions for a single variable that lead to similar actions.

Would you like an example with a grouped switch involving more complex expressions or user input validation?

Post a Comment

0 Comments