SWITCH:-
when we needseveral if statements,it is better to use switch statement. in other words, switch is a variation of if statement which performs multiway branching.
- syntax:-
switch(expression){
case value:
statement(s);
break;
case value:
statement(s);
break;
default:
statement(s);
break;
}
switch is key word begins switch construct.
break breaks exection of perticular statement.
- code:-
#include<iostream.h>
void main()
{
int k;
cout<<"press any key"<<endl;
cin>>k;
switch(k)
{
case 1 : cout<<"one"<<endl;
break;
case 2 : cout<<"two"<<endl;
break;
case 3:cout<<"three"<<endl;
break;
default : cout<<"please enter the valid entry"<<endl;
break;
}
}
note:-it's just a sudo code if its not executing on your device make some changes in the further code this is only for understanding logic.
0 Comments