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
  • 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...

  • 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 +...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division...

    Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division by zero and invalid input. Your program should print Denominator must be nonzero and reprompt for a valid denominator when 0 is entered for a denominator. Please specify what should go in the divisionByZero.h file and the changes made to main.cpp Please provide output. Thank you in advance! main.cpp so far #include <iostream> using namespace std; void addFractions(int num1, int num2, int den1, int...

  • C++ Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money...

    C++ Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money market, to attract customers as well as meet their specific needs. Two of the most commonly used accounts are savings and checking. Each of these accounts has various options. For example, you may have a savings account that requires no minimum balance but has a lower interest rate. Similarly, you may have a checking account that limits the number of checks you may write....

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
ADVERTISEMENT