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:
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:
#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; }
Enter The Month in Number (1-12)10 OctoberTo download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions