Question

Question 5: Consider the following C++ program. switch(x) case 1: if (x > 1) case 2: if (x > 3) case 3: cout<<Epic Beats! <

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In switch case,if we didn't write the break for each case.Then the program continues to next case ,executing the statements untill a break.

A). For x =1: Output = Chill Vibes

If x =1 :it will go to case 1: Without break switch will be look like below.

case 1:

if(x>1)

{

if(x>2)

{   

cout<< "Epic Beats!"<<endl;

}

}

cout << "Chill Vibes"<<endl;

break;

So it check the condition x>1 (1>1).It is false.So it doesn't goes to next statement if(x>2).So it doesn't execute the cout<< "Epic Beats!". Next statement is cout << "Chill Vibes";

So for x = 1.It prints Chill Vibes.

B). For x =3: Output :

Epic Beats!

Chill Vibes

If x =3:it will go to case 3: Without break switch will be look like below.

case 3:

cout<< "Epic Beats!"<<endl;

cout << "Chill Vibes"<<endl;

break;

Tt executes the statement cout<< "Epic Beats!";.It prints Epic Beats! and next statement is  cout << "Chill Vibes";.It prints Chill Vibes.

So for x = 3.It prints

Epic Beats!

Chill Vibes

C). For x =2: Output = Chill Vibes

If x =2 :it will go to case 2: Without break switch will be look like below.

case 2:

if(x>2)

{   

cout<< "Epic Beats!"<<endl;

}

cout << "Chill Vibes"<<endl;

break;

In this code checks the x>2(2>2).This condition is false.So it doesn't print Epic Beats!.Next it executes the statemen cout << "Chill Vibes";.It prints Chill Vibes

D). For x =9: Output = Invalid input.Try again.

In switch case , If x value is not equal to any case.Then switch case execute the default case.

For x =9,default will execute.So it executes cout<<"Invalid input.Try again."<<endl;

It prints the Invalid input.Try again.

Add a comment
Know the answer?
Add Answer to:
Question 5: Consider the following C++ program. switch(x) case 1: if (x > 1) case 2:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The switch Statement C++program This lab work with the switch statement. • Remove the break statements...

    The switch Statement C++program This lab work with the switch statement. • Remove the break statements from each of the cases. What is the effect on the execution of the program? • Add an additional switch statement that allows for a Passing option for a grade of D or better. Use the sample run given below to model your output. Sample Run: What grade did you earn in Programming I? YOU PASSED! An A - excellent work! The following is...

  • give the output of the codes in C++ int g(int x) { int tmp = x...

    give the output of the codes in C++ int g(int x) { int tmp = x + 1; return x* tmp; int fint x, int y) { X += Y; return g(x); int main() { int x = 3; cout << g(x) << endl; cout << f(x, 1) << endl; cout << f(x, 2) << endl; int intVar = 24.2/2; cout<<intVar; Int main() int Enter = 1; switch (Enter) case 1: Enter = -4; case 2: Enter = -6; case...

  • I am working on this switch program everything seems to be ok but the compiler is...

    I am working on this switch program everything seems to be ok but the compiler is giving me an error on the final closing brace and wanted to know where I am making an error #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int choice; char repeat; double MidTerm = 0; double FinalExam = 0; double Quiz1 = 0; double Quiz2 = 0; double MidTermGrade = 1, FinalExamGrade = 2, Quiz1Grade = 3, Quiz2Grade = 4,...

  • 1. What would be the output of the following lines of code: int num = 2;...

    1. What would be the output of the following lines of code: int num = 2; switch(num){ case 1: cout << "1"; case 2: cout << "2"; case 3: cout << "3"; case 4: cout << "4"; } 2. What would be the output of the following code: char op = '*'; switch(op){ case '+': cout << "Addition"; break; case '-': cout << "Subtraction"; break; case '/': cout << "Division"; break; default: cout << "Invalid"; } 3. What would be...

  • Hello i am having a bit of trouble with a verified exit for my program. For...

    Hello i am having a bit of trouble with a verified exit for my program. For some reason when trying to exit it loops to the team selection function? C++ Visual Studio 2017 #include "cPlayer.h" char chChoice1 = ' '; char chChoice3 = ' '; cPlayer::cPlayer() { } cPlayer::~cPlayer() { } void cPlayer::fMenu() {    char chChoice3 = ' ';    do    {        cout << "\n\t--Menu--" << endl;        cout << "1) Enter Player Name" <<...

  • C++ programming I need at least three test cases for the program and at least one...

    C++ programming I need at least three test cases for the program and at least one test has to pass #include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; void temperatureCoverter(float cel){ float f = ((cel*9.0)/5.0)+32; cout <<cel<<"C is equivalent to "<<round(f)<<"F"<<endl; } void distanceConverter(float km){ float miles = km * 0.6; cout<<km<<" km is equivalent to "<<fixed<<setprecision(2)<<miles<<" miles"<<endl; } void weightConverter(float kg){ float pounds=kg*2.2; cout<<kg<<" kg is equivalent to "<<fixed<<setprecision(1)<<pounds<<" pounds"<<endl; } int main() { string country;...

  • can you show me the details please. thank you 13. What is the output of the...

    can you show me the details please. thank you 13. What is the output of the following program? #include <iostream.h> void main() { int x; 1/ declare variable for (x = 1; x <= 5; x++) { // loop 5 times switch (x) { case 1: cout << x < // switch value of // if "x" is equal // display "X", but endl; case 2: cout << x << endl; // if "x" is equal // display "x", but...

  • PSUEDOCODE ONLY!! What is the output of the following code? CREATE x 1 SWITCH (x) Print...

    PSUEDOCODE ONLY!! What is the output of the following code? CREATE x 1 SWITCH (x) Print output here: Print output here: BREAK CASE 2: PRINTLINE (“hELLO wORLD”) CASE 3: PRINT (“HELLO WORLD”) BREAK CASE 4: PRINTLINE(“Hello”) CASE 5: PRINT(“World”) BREAK DEFAULT: BREAK

  • 41. Executing the "continue" statement from within a loop     causes control to go     A....

    41. Executing the "continue" statement from within a loop     causes control to go     A. to the next line of code     B. out of the loop     C. to the beginning of the loop          D. to check the loop condition for repeating the loop     E. none of the above      42. The 'default' statement inside a 'switch()' is optional.          T__   F__             43. The following 'switch' implementation is legal:              int i = 2;        ...

  • Covert C++ code into MIPS. -If x has a value if 2, print "bbb" if x...

    Covert C++ code into MIPS. -If x has a value if 2, print "bbb" if x has a value of 3, print "ccc" if x has a value of 4, print "ddd" if x has a value other than 2, 3, or 4 print "eee". result = ""; switch (x) { case 2: result = result + "bbb"; break; case 3: result = result + "ccc"; break; case 4: result = result + "ddd"; break; default: result = result +...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT