ASSIGNMENT3
(SWITCH..CASE,IF..ELSE..,WHILE LOOP)
QUESTIONS AND ANSWERS
Q1. Rewrite the if ..else program to switch..case:
if (x == 1 || x == 2)
{
cout<<"Its 1 and 2 selected";
}else if (x == 3 || x == 4)
{
cout<<"Its 3 and 4 selected";
}else
{
cout<<"Not selected";
}
Q2. Rewrite the switch..case to if..elseif..else
switch(option)
{
case 'a':
case 'A': a=20+10;
cout<<"Addition process result"<<a;
break;
case 'b':
case 'B': a=20-10;
cout<<"Subtraction process result:"<<a;
break;
case 'c':
case 'C': a=20*10;
cout<<"Multiplication process result:"<<a;
break;
case 'd':
case 'D': a=20/10;
cout<<"Division process result:"<<a;
break;
default: cout<<"Invalid option";
}
{
case 'a':
case 'A': a=20+10;
cout<<"Addition process result"<<a;
break;
case 'b':
case 'B': a=20-10;
cout<<"Subtraction process result:"<<a;
break;
case 'c':
case 'C': a=20*10;
cout<<"Multiplication process result:"<<a;
break;
case 'd':
case 'D': a=20/10;
cout<<"Division process result:"<<a;
break;
default: cout<<"Invalid option";
}
Q3. Rewrite the switch..case to if..elseif..else
switch (number)
{
case 1:
cout<<"My Favourite Subject is";
break;
case 2:
cout<<"Fundamentals of Programming";
break;
case 3:
cout<<"Exit";
break;
default:
cout<<"Invalid Data";
}
{
case 1:
cout<<"My Favourite Subject is";
break;
case 2:
cout<<"Fundamentals of Programming";
break;
case 3:
cout<<"Exit";
break;
default:
cout<<"Invalid Data";
}
Q4. Rewrite the switch..case to if..elseif..else
switch (x)
{
case 1:
case 2:
case 3:
cout << "x is 1, 2 or 3";
break;
default:
cout << "x is not 1, 2 nor 3";
}
case 1:
case 2:
case 3:
cout << "x is 1, 2 or 3";
break;
default:
cout << "x is not 1, 2 nor 3";
}
Q5. Rewrite the if ..else program to switch..case:
if(choice == 1)
{
cout<<"You selected 1.";
}
else if(choice == 2 || choice == 3)
{
cout<<"You selected 2 or 3.";
}
else if(choice == 4)
{
cout<<"You selected 4.";
}
else
{
cout<<"Please enter a choice between 1-4.";
}
{
cout<<"You selected 1.";
}
else if(choice == 2 || choice == 3)
{
cout<<"You selected 2 or 3.";
}
else if(choice == 4)
{
cout<<"You selected 4.";
}
else
{
cout<<"Please enter a choice between 1-4.";
}
Q6. Rewrite the if ..else program to switch..case:
char wish;
if(wish=='a')
cout<<"YOU WILL GET 40 OUT OF 40";
else if(wish=='b' || wish=='c')
cout<<"MY FRIEND WILL GET 40 OUT OF 40";
else if(wish=='d')
cout<<"TEACHER WILL NOT GIVE 40 OUT OF 40";
else
cout<<"NO ONE WILL GET 40 OUT OF 40";
if(wish=='a')
cout<<"YOU WILL GET 40 OUT OF 40";
else if(wish=='b' || wish=='c')
cout<<"MY FRIEND WILL GET 40 OUT OF 40";
else if(wish=='d')
cout<<"TEACHER WILL NOT GIVE 40 OUT OF 40";
else
cout<<"NO ONE WILL GET 40 OUT OF 40";
Q7. Rewrite the if ..else program to switch..case:
if(code=='A')
cout<<"Autumn";
else
if(code=='B')
cout<<"Winter";
else
if(code=='C' || code=='c')
cout<<"Spring";
else
cout<<"Enter the correct code";
cout<<"Autumn";
else
if(code=='B')
cout<<"Winter";
else
if(code=='C' || code=='c')
cout<<"Spring";
else
cout<<"Enter the correct code";
Q8. Rewrite the if ..else program to switch..case:
if(ch=='E' || ch=='e')
cout<<"EAST";
else if(ch=='W')
cout<<"WEST";
else if(ch=='N')
cout<<"NORTH";
else if(ch=='S')
cout<<"SOUTH";
else
cout<<"Incorrect direction";
cout<<"EAST";
else if(ch=='W')
cout<<"WEST";
else if(ch=='N')
cout<<"NORTH";
else if(ch=='S')
cout<<"SOUTH";
else
cout<<"Incorrect direction";