Switch Case:- It tests a condition or control expression. According to this control expression the control is transferred to one of the several cases. The values of expression may be of type int and char but not of type float or double.
There are some points which we have to remembered when switch statement is used:-
- Always put a break statement after the case statement in a switch .
- Two case constants in a switch statement cannot have the same value, but the same statements can be executive for two or more different case.
Here below a Program that will show you that how to print Number of Days in a Month using switch case.
#include<iostream.h>
#include<conio.h>
main ()
{
int n;
cout<<"enter the number of days";
cin>>n;
switch(n)
{
case 1:
case 3:
case 5:
case 7:
case 9:
case 10:
case 12: cout<<"there are 31 days ";
break;
case 4:
case 6:
case 8:
case 11: cout<<"there are 30 days";
break;
case 2: cout<<"there is only 28 days in this month";
break;
default: cout<<"invalid";
}
getch();
}
Output will be:-
There are some points which we have to remembered when switch statement is used:-
- Always put a break statement after the case statement in a switch .
- Two case constants in a switch statement cannot have the same value, but the same statements can be executive for two or more different case.
Here below a Program that will show you that how to print Number of Days in a Month using switch case.
#include<iostream.h>
#include<conio.h>
main ()
{
int n;
cout<<"enter the number of days";
cin>>n;
switch(n)
{
case 1:
case 3:
case 5:
case 7:
case 9:
case 10:
case 12: cout<<"there are 31 days ";
break;
case 4:
case 6:
case 8:
case 11: cout<<"there are 30 days";
break;
case 2: cout<<"there is only 28 days in this month";
break;
default: cout<<"invalid";
}
getch();
}
Output will be:-
No comments:
Post a Comment