The switch statement is designed to solve what kind of probl
Solution
solution:
given program
//include #include<iostream> otherwise cin,cout won\'t work it shows compiletime error (cout,cin not declared in this //scope)
using namespace std;
void main() //syntax error write int main()
{
double num1,num2,sum;
cout<<\"enter a number\";
cin>>num1;
cout<<\"enter a number\";
cin>>num2;
num1+num2=sum;//result should be left assignment
cout<<\"sum\"<<sum;
}
correct program
#include<iostream>
using namespace std;
int main()
{
double num1,num2,sum;
cout<<\"enter num1\";
cin>>num1;
cout<<\"enter num2\";
cin>>num2;
sum=num1+num2;
cout<<sum;
return 0;
}
output
enter num1
10
enter num2
20
sum 30
to find the expression value
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double g,h,k,t,s,m,a;
cout<<\"enter h value\ \";
cin>>h;
cout<<\"enter k value\ \";
cin>>k;
cout<<\"enter the value of a\";
cin>>a;
t=h+12;//to find h+12 value
s=pow(a,10); //to compute the power of a^10
m=4*k*s; //to calculate the 4k*a^10
g=(t+m)/(4*k);//to find the g value
cout<<\"the given expression result is\ \"<<g;
}
output
enter h value
3
enter k value
6
enter a value
4
the given expression result g is 1.04858e+006
the differences between switch and if-else ladder
1)switch works faster than if-else.it takes expression and quickly jump starts execution .
2)if more statements to evaluate use switch rather than if-else ladder
3)to execute flat expressions use if-else ladder
4)simple or few number of expressions use if-else ladder
5)switch gives more performance when compare with if-else ladder

