Signup/Sign In

C++ Switch Case with break Statement Program

Hello Everyone!

In this tutorial, we will learn how to demonstrate the concept of Switch Case with break statement, in the C++ programming language.

C++ Switch Case:

In Programming, a Switch Case is just an alternative for the multiple if-else blocks. It is used to execute the block of the code only when a particular condition is fulfilled. A break statement is used to stop the code flow from entering into the remaining blocks and hence making it directly move out of the switch block when even a single condition is fulfilled.

For better understanding, we will highly recommend you to visit one of our posts here: https://www.studytonight.com/c/programs/basic/switch-case, where we have discussed this concept in detail.

Code:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to demonstrate the concept of Switch Case with break statement, in CPP  ===== \n\n";

    //variable to store the operation to be performed
    char operation;

    //variable to store the operands
    int n1, n2, result;

    cout << "\nEnter the operation you want to perform: either + or - or * or / or %: ";
    cin >> operation;

    cout << "\n\n\nEnter the two operands you want to perform the operation on: \n";
    cin >> n1 >> n2;

    //Logic of Switch case with break statement
    switch (operation)
    {
    case '+':
        result = n1 + n2;
        break;
    case '-':
        result = n1 - n2;
        break;
    case '*':
        result = n1 * n2;
        break;
    case '/':
        result = n1 / n2;
        break;
    case '%':
        result = n1 % n2;
        break;

    default:
        cout << "\n\nPlease Enter a Valid Operation from the mentioned list";
        break;
    }

    cout << "\n\nResult obtained from the given data is: " << n1 << operation << n2 << " = " << result << "\n\n\n ";
    return 0;
}

Output:

C++ switch case with break

We hope that this post helped you develop a better understanding of the concept of Switch Case with a break statement and its implementation in C++. For any query, feel free to reach out to us via the comments section down below.

Keep Learning : )



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.