Switch Statement in C++ Programming


The switch statement is c++ multi-way branch statement. It is used to take the place of long if-else chains, and make them more readable. However, unlike if statements, one may not use inequalities; each value must be concretely defined.

There are three critical components to the switch statement:

  • Case: This is the value that is evaluated for equivalence with the argument to the switch statement.
  • Default:This is an optional, catch-all expression, should none of the case statements evaluate to true.
  • Abrupt completion of the case statement; usually break: This is required to prevent the undesired evaluation of further case statements

The program takes an integer input from the user representing a month number and uses a switch statement to print the corresponding month name. If the user enters a number outside the range of 1-12, the program prints "Invalid Month Value". Here is a breakdown of the program:

  • The first line includes the iostream header, which allows the program to input and output data.
  • The program defines the main function which returns an integer.
  • The program declares an integer variable 'm'.
  • The program outputs a message asking the user to enter a month number between 1 and 12.
  • The program uses the 'cin' statement to take user input and assigns it to the 'm' variable.
  • The program uses a switch statement to compare the value of 'm' to various cases. If 'm' matches one of the cases, the corresponding month name is printed. If 'm' doesn't match any of the cases, the default case is executed, printing "Invalid Month Value".
  • The 'break' statement is used after each case statement to break out of the switch block and prevent execution of the following cases.
  • The 'return 0' statement is used to exit the main function and return a value of 0, indicating successful completion of the program.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int m;
    cout<<"\nEnter The Month in Number (1-12)";
    cin>>m;
    switch(m)
    {
    case 1:
        cout<<"January"<<endl;
        break;
    case 2:
        cout<<"February"<<endl;
        break;
    case 3:
        cout<<"March"<<endl;
        break;
    case 4:
        cout<<"April"<<endl;
        break;
    case 5:
        cout<<"May"<<endl;
        break;
    case 6:
        cout<<"June"<<endl;
        break;
    case 7:
        cout<<"July"<<endl;
        break;
    case 8:
        cout<<"August"<<endl;
        break;
    case 9:
        cout<<"September"<<endl;
        break;
    case 10:
        cout<<"October"<<endl;
        break;
    case 11:
        cout<<"November"<<endl;
        break;
    case 12:
        cout<<"December"<<endl;
        break;
    default:
        cout<<"Invalid Month Value"<<endl;
        break;
    }
    return 0;
}
 
 

Output

Enter The Month in Number (1-12)10
October
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples