4. Convert the following switch statement into an if/else if statement. switch (
ID: 3890075 • Letter: 4
Question
4. Convert the following switch statement into an if/else if statement. switch (choice) t case 1: charges months40; break; case 2: case 3 case 4: charges months 20; break; case 5: case G case 7: case 8 case 9: charges months10; break; default: charges-1; break; // end switeh 5. What is the output of following code segment if choice contains the value 1 and months contains the value 5? switch (choice) t case 1: charges months40; case 2: case 3 case 4: charges months 20; case 5: case 6: case 7: case 8 case 9: charges months10; coutExplanation / Answer
Hi Let me know if you need more information:-
==================================================
#include <iostream>
using namespace std;
int main() {
int charges = 20;
int months = 5;
int choice = 0;
//////////////////////////////////////////////////////
switch (choice) {
case 1:
charges = months * 40;
break;
case 2:
case 3:
case 4:
charges = months * 20;
break;
case 5:
case 6:
case 7:
case 8:
case 9:
charges = months * 10;
break;
default:
charges = -1;
break;
}
//////////////////////////////////////////////////////
////////////////////IF-ELSE//////////////////////////////////
//switch (choice) {
if (choice == 1) {
charges = months * 40;
} else if (choice == 2) {
} else if (choice == 3) {
} else if (choice == 4) {
charges = months * 20;
}
else if (choice == 5) {
} else if (choice == 6) {
} else if (choice == 7) {
} else if (choice == 8) {
} else if (choice == 9) {
charges = months * 10;
} else {
charges = -1;
}
//}
//////////////////IF-ELSE////////////////////////////////////
///////////////////////START///////////////////////////////
choice = 1;
months = 5;
switch (choice) {
case 1:charges = months * 40; //Charges 5*40 = 200
case 2:
case 3:
case 4:charges = months * 20;//Charges 5*20 = 100
case 5:
case 6:
case 7:
case 8:
case 9: charges = months * 10; //Charges 5*10 = 50
}
cout << "Your total charges are $ " << charges << endl;
//OUTPUT: Your total charges are $ 50
return 0;
///////////////////////END///////////////////////////////
}
===============================
NOTE:-
================================
when you not given break statement it wont return from the case...
so in above case it will come till last statement.
========================================
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.