Switch case in C++ by R4R Team

Basically,Switch case work as decision making statement in C and C++ language.
Syntax-
switch(variable)
{
case value1:
body;
case value2:
body;
.
.
.
}
Now we can guess number is 1,2 or 3.

program:

#include < iostream>
using namespace std;
class myclass
{
public:
//guess the number
int guess(int a)
{
switch(a)
{
case 1:
cout<<"Number is 1";
break;
case 2:
cout<<"Number is 2";
break;
case 3:
cout<<"Number is 3";
break;
default:
cout<<"Undefined";
}
}
};
int main()
{
int n;
cout<<"Enter any number";
cin>>n;
myclass obj;
obj.guess(n);
return 0;
}


output-

Enter any number
2
Number is 2

Enter any number
34
Undefined

-In this program, we take an input of one number through the user then pass in guess function inside class that guess number is 1,2 or 3.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!