I will discuss few things here.
- Switch statement
- nested switch statement
- exercises
If some one say test any variable defined for some value against different or list of values , then there switch statement makes it easy.It is just like comparison phenomenon.
switch ( expression)
{
case constant_expression :
statements;
break;
case constant_expression :
statements;
break;
.
.
.
.
default :
statements;
}
Now the value of variable which is to be checked is place in switch 's expression , and the list of values against which the variable is to be checked defined as case expression values.
Terms used in the syntax :
Expression: Expression must be of integral or literal or class type.
Case: There may be any number of case statements you can define followed by the constant expression and colon.
Constant_expression: The data type of constant_expression defined in cases must be same as variable in the switch.It must be constant or literal.
Break: When the case value found equal to value of variable in the switch , then statements inside that case execute till the break statement.
Here the break statement is optional , No break is required by every case,the flow of control jumps to next line in the switch statement.
Default: Optional default case is also there, If no case found to be correct or equal , then for sake of convenience or for the programmer that he/she know that there is no case found to be correct.
Example:
Now lets take an example,i will make program for students to check their examination performance. Students will check their performance depending upon their grades they obtained form grades value A ,B and C.
The student will enter his grade he/she obtained and he will notified about his performance
Grade
|
Performance
|
A
|
Excellent
|
B
|
Well done
|
C
|
Good
|
Program Code:
Note: Click on the images to enlarge them.
Output:
After compilation and execution of above program the output can be as follows
Nested Switch statements
Now if you have learnt the switch statement , you can easily learn nested switch statements from its syntax.
Syntax
switch()
{
case 1:
statements;
switch()
{
case 1:
statements;
break;
case 2:
statements;
}
break;
case 2:
statements;
}
C++ specifies that you can specify at least 256 levels of nesting.
Keep practicing on it.
Exercises:
1.What is the output of the following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
char x;
cout<< " Enter your Grade from A to C "<<endl<<endl;
cin>>x; // enter any grade from A, B or C
switch (x)
{
case 'A':
cout<<"your performance is Execellent"<<endl<<endl;
break;
case 'D':
cout<<"your performance is Well done"<<endl<<endl;
break;
case 'C':
cout<<"your performance is Good"<<endl<<endl;
default:
cout<<" you have entered invalid grade "<<endl<<endl;
}
getch();
}
2.What is the output of the following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
char x;
cout<< " Enter your Grade from A to C "<<endl<<endl;
cin>>x; // enter any grade from A, B or C
switch (x)
{
case 'A':
cout<<"your performance is Execellent"<<endl<<endl;
break;
case 'D':
cout<<"your performance is Well done"<<endl<<endl;
break;
case 'E':
cout<<"your performance is Good"<<endl<<endl;
default:
cout<<" you have entered invalid grade "<<endl<<endl;
}
getch();
}
3.What is the output of the following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
char x;
cout<< " Enter your Grade from A to C "<<endl<<endl;
cin>>x; // enter any grade from A, B or C
switch (x)
{
case 'A':
cout<<"your performance is Execellent"<<endl<<endl;
break;
case 'B':
cout<<"your performance is Well done"<<endl<<endl;
break;
case 'E':
cout<<"your performance is Good"<<endl<<endl;
default:
cout<<" you have entered invalid grade "<<endl<<endl;
}
getch();
}
4.What is the output of the following program ? if user enter 1
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main ()
{
float x= 2.83;
int y;
cout<<"Enter your choice 1=sin x ,2=cosx , 3=tanx , 4 = log x" <<endl<<endl;
cin>>y;
switch(y)
{
case 1:
cout<<sin(x);
break;
case 2:
cout<<cos(x);
break;
case 3:
cout<<tan(x);
case 4:
cout<<log(x);
default:
cout<<endl<<"invalid number you have entered";
}
getch();
}
5.What is the output of the following program ? if user enter 2
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main ()
{
float x= 2.83;
int y;
cout<<"Enter your choice 1=sin x ,2=cosx , 3=tanx , 4 = log x" <<endl<<endl;
cin>>y;
switch(y)
{
case 1:
cout<<sin(x);
break;
case 2:
cout<<cos(x);
break;
case 3:
cout<<tan(x);
case 4:
cout<<log(x);
default:
cout<<endl<<"invalid number you have entered";
}
getch();
}
6.What is the output of the following program ?if user enter 3
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main ()
{
float x= 2.83;
int y;
cout<<"Enter your choice 1=sin x ,2=cosx , 3=tanx , 4 = log x" <<endl<<endl;
cin>>y;
switch(y)
{
case 1:
cout<<sin(x);
break;
case 2:
cout<<cos(x);
break;
case 3:
cout<<tan(x);
case 4:
cout<<log(x);
default:
cout<<endl<<"invalid number you have entered";
}
getch();
}
7.What is the output of the following program ?if user enter 6
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main ()
{
float x= 2.83;
int y;
cout<<"Enter your choice 1=sin x ,2=cosx , 3=tanx , 4 = log x" <<endl<<endl;
cin>>y;
switch(y)
{
case 1:
cout<<sin(x);
break;
case 2:
cout<<cos(x);
break;
case 3:
cout<<tan(x);
case 4:
cout<<log(x);
default:
cout<<endl<<"invalid number you have entered";
}
getch();
}
8.What is the output of the following program ?if user enter 9
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main ()
{
float x= 2.83;
int y;
cout<<"Enter your choice 1=sin x ,2=cosx , 3=tanx , 4 = log x" <<endl<<endl;
cin>>y;
switch(y)
{
case 1:
cout<<sin(x);
break;
case 2:
cout<<cos(x);
break;
case 3:
cout<<tan(x);
case 4:
cout<<log(x);
default:
cout<<endl<<"invalid number you have entered";
}
getch();
}
9.What is the output of the following program ?if user enter 4
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main ()
{
float x= 2.83;
int y;
cout<<"Enter your choice 1=sin x ,2=cosx , 3=tanx , 4 = log x" <<endl<<endl;
cin>>y;
switch(y)
{
case 1:
cout<<sin(x);
break;
case 2:
cout<<cos(x);
break;
case 3:
cout<<tan(x);
case 4:
cout<<log(x);
default:
cout<<endl<<"invalid number you have entered";
}
getch();
}
10.What is the output of the following program ? if user enter 0
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main ()
{
float x= 2.83;
int y;
cout<<"Enter your choice 1=sin x ,2=cosx , 3=tanx , 4 = log x" <<endl<<endl;
cin>>y;
switch(y)
{
case 1:
cout<<sin(x);
break;
case 2:
cout<<cos(x);
break;
case 3:
cout<<tan(x);
case 4:
cout<<log(x);
default:
cout<<endl<<"invalid number you have entered";
}
getch();
}
if we define variable x ourself then what is procedure?
ReplyDeleteSir how to write program for vowels using switch case in dev c++ plz help me.
ReplyDelete