switch case statement

 It looks like you're building a C++ tutorial with practical examples! Here’s a structured breakdown of the switch case statement along with different use cases.


🛠️ Switch Case in C++

The switch case statement is used when we have multiple conditions to check against a single variable.

Key Features of Switch Case:
✔ Compares a variable against multiple values (case labels).
✔ Uses break to exit after a match.
✔ Uses default for unmatched cases.


1️⃣ Basic Switch Case Example

📌 Example: A simple menu system using switch.

#include <iostream>
using namespace std;

int main() {
    int choice;
    cout << "Enter your choice (1-3): ";
    cin >> choice;

    switch (choice) {
        case 1:
            cout << "You chose option 1." << endl;
            break;
        case 2:
            cout << "You chose option 2." << endl;
            break;
        case 3:
            cout << "You chose option 3." << endl;
            break;
        default:
            cout << "Invalid choice!" << endl;
    }

    return 0;
}

🔹 Output (if input is 2)

Enter your choice (1-3): 2
You chose option 2.

2️⃣ Calculator using Switch Case

📌 Example: A calculator that performs basic arithmetic operations.

#include <iostream>
using namespace std;

int main() {
    char op;
    double num1, num2;

    cout << "Enter operator (+, -, *, /): ";
    cin >> op;
    cout << "Enter two numbers: ";
    cin >> num1 >> num2;

    switch (op) {
        case '+':
            cout << "Result: " << num1 + num2 << endl;
            break;
        case '-':
            cout << "Result: " << num1 - num2 << endl;
            break;
        case '*':
            cout << "Result: " << num1 * num2 << endl;
            break;
        case '/':
            if (num2 != 0)
                cout << "Result: " << num1 / num2 << endl;
            else
                cout << "Error! Division by zero." << endl;
            break;
        default:
            cout << "Invalid operator!" << endl;
    }

    return 0;
}

🔹 Output (if input is + 5 3)

Enter operator (+, -, *, /): +
Enter two numbers: 5 3
Result: 8

3️⃣ Present Elevator Position in C++

📌 Example: Displaying elevator position using switch.

#include <iostream>
using namespace std;

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

    switch (floor) {
        case 1:
            cout << "Elevator is on the 1st floor." << endl;
            break;
        case 2:
            cout << "Elevator is on the 2nd floor." << endl;
            break;
        case 3:
            cout << "Elevator is on the 3rd floor." << endl;
            break;
        case 4:
            cout << "Elevator is on the 4th floor." << endl;
            break;
        case 5:
            cout << "Elevator is on the 5th floor." << endl;
            break;
        default:
            cout << "Invalid floor number!" << endl;
    }

    return 0;
}

🔹 Output (if input is 3)

Elevator is on the 3rd floor.

4️⃣ Hotel Management System (Room Type Selection)

📌 Example: Choose a room type using switch.

#include <iostream>
using namespace std;

int main() {
    int roomType;
    cout << "Select Room Type (1: Standard, 2: Deluxe, 3: Suite): ";
    cin >> roomType;

    switch (roomType) {
        case 1:
            cout << "You selected Standard Room - $100 per night." << endl;
            break;
        case 2:
            cout << "You selected Deluxe Room - $150 per night." << endl;
            break;
        case 3:
            cout << "You selected Suite - $250 per night." << endl;
            break;
        default:
            cout << "Invalid Room Type!" << endl;
    }

    return 0;
}

🔹 Output (if input is 2)

You selected Deluxe Room - $150 per night.

5️⃣ Display Month in C++

📌 Example: Display the month name based on input (1-12).

#include <iostream>
using namespace std;

int main() {
    int month;
    cout << "Enter month number (1-12): ";
    cin >> month;

    switch (month) {
        case 1: cout << "January"; break;
        case 2: cout << "February"; break;
        case 3: cout << "March"; break;
        case 4: cout << "April"; break;
        case 5: cout << "May"; break;
        case 6: cout << "June"; break;
        case 7: cout << "July"; break;
        case 8: cout << "August"; break;
        case 9: cout << "September"; break;
        case 10: cout << "October"; break;
        case 11: cout << "November"; break;
        case 12: cout << "December"; break;
        default: cout << "Invalid Month";
    }

    return 0;
}

🔹 Output (if input is 5)

May

6️⃣ Find Vowels in C++

📌 Example: Check if a character is a vowel.

#include <iostream>
using namespace std;

int main() {
    char ch;
    cout << "Enter a letter: ";
    cin >> ch;

    switch (ch) {
        case 'a': case 'e': case 'i': case 'o': case 'u':
        case 'A': case 'E': case 'I': case 'O': case 'U':
            cout << "It is a vowel." << endl;
            break;
        default:
            cout << "It is a consonant." << endl;
    }

    return 0;
}

🔹 Output (if input is 'o')

It is a vowel.

7️⃣ Find Weekend in C++

📌 Example: Identify if a day is a weekend or a weekday.

#include <iostream>
using namespace std;

int main() {
    int day;
    cout << "Enter day number (1-7, where 1 = Sunday): ";
    cin >> day;

    switch (day) {
        case 1:
        case 7:
            cout << "It's a weekend!" << endl;
            break;
        case 2: case 3: case 4: case 5: case 6:
            cout << "It's a weekday." << endl;
            break;
        default:
            cout << "Invalid day number!" << endl;
    }

    return 0;
}

🔹 Output (if input is 6)

It's a weekday.

📌 Summary

switch case is useful for menu-driven programs.
break stops execution after a case is matched.
default handles invalid inputs.
✔ Useful in calculators, hotel booking, weekday/weekend check, etc.

Would you like more advanced switch case problems? 🚀

Post a Comment

0 Comments