Switch Statement in C++

Here are the example of Switch Statement in C++ language:

C++
#include <iostream>
using namespace std;

int main()
{
    int day = 2;
    switch (day) {
    case 1:
        cout << "Monday";
        break;
    case 2:
        cout << "Tuesday";
        break;
    case 3:
        cout << "Wednesday";
        break;
    case 4:
        cout << "Thursday";
        break;
    case 5:
        cout << "Friday";
        break;
    case 6:
        cout << "Saturday";
        break;
    case 7:
        cout << "Sunday";
        break;
    default:
        cout << "Not a valid number";
    }
    return 0;
}

Output
Tuesday

Switch statement in Programming

Switch statement in programming enables the execution of different code blocks based on the value of an expression, providing a structured approach to handle multiple cases efficiently. It enhances code readability and simplifies decision-making processes, making it a valuable tool for managing program flow and facilitating branching logic in software development.

Table of Content

  • What is Switch Statement?
  • Syntax and Structure of Switch Statement
  • Case Statement
  • Break Statement
  • Default Statement
  • Switch Statement in C
  • Switch Statement in C++
  • Switch Statement in Java
  • Switch Statement in Python
  • Switch Statement in C#
  • Switch Statement in JavaScript

Similar Reads

What is Switch Statement?

Switch statement is a fundamental building block in many programming languages. It allows us to efficiently handle situations where the program needs to perform different actions based on different values of a variable....

Syntax and Structure of Switch Statement:

A switch statement typically consists of the following elements....

Case Statement:

Each case statement defines a possible value the switch expression can take. The syntax usually involves the case keyword followed by the constant value....

Break Statement:

The break statement is an important element within the switch block. After a matching case is executed, the break statement terminates the switch block entirely, preventing the program from accidentally falling through and executing code from subsequent cases. This is also called fallthrough behaviour....

Default Statement:

The default statement in programming is a fallback option used in switch statements. It is executed when none of the case labels match the value of the expression being evaluated....

Switch Statement in C:

Here are the example of Switch Statement in C language:...

Switch Statement in C++:

Here are the example of Switch Statement in C++ language:...

Switch Statement in Java:

Here are the example of Switch Statement in java language:...

Switch Statement in Python:

Before Python 3.10, there was no feature for Switch statement in Programming. In Python 3.10, they have included match and case statement to implement Switch Case in Programming. Instead of default: case, we write case _: and there is no need to write break statement after each case....

Switch Statement in C#:

Here are the example of Switch Statement in C# language:...

Switch Statement in JavaScript:

Here are the example of Switch Statement in javascript language:...

Conclusion:

The switch statement in programming allows selecting different code paths based on a variable’s value, providing a structured approach for handling multiple cases efficiently. While beneficial for readability and decision-making, it’s crucial to use switch statements judiciously and explore alternative strategies for complex logic....