Signup/Sign In

C++ Switch Case without Break Statement Program

Hello Everyone!

In this tutorial, we will learn how to demonstrate the concept of Switch case without 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.

Without a break statement, the code gets into each of the cases following the case that matches. The example discussed in the below code will help you understand the functionality provided by the break statement within a switch case.

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 without break statement, in CPP  ===== \n\n";

    //variable to run the Switch case with
    int sw = 1;

    cout << "\n\n***** Entering the Switch case with value 1 ***** \n\n";

    //Logic of Switch case with break statement
    switch (sw)
    {
    case 1:
        cout << "\nInside Case 1\n";
    case 2:
        cout << "\nInside Case 2\n";
    case 3:
        cout << "\nInside Case 3\n";
    case 4:
        cout << "\nInside Case 4\n";
    case 5:
        cout << "\nInside Case 5\n";
    default:
        cout << "\nInside the Default Case\n";
    }

    cout << "\n\n***** Exiting the Switch case ***** \n\n\n";
    return 0;
}

Output:

C++ switch without break

We hope that this post helped you develop a better understanding of the concept of Switch Case without a break statement, 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.