Question

C++ Program What is the output of the following code fragment?(beta is of type int.) beta...

C++ Program

What is the output of the following code fragment?(beta is of type int.)

beta = 5;

do

{

switch (beta)

{

case 1: cout <<'R'; break;

case 2: cout

case 4: cout << 'O'; break;

case 5: cout << 'L';

}

beta--;

}while (beta>1);

cout <<'X';

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

Statement inside case 2 of switch is missing in the question. Please provide the statement in a comment to get the correct answer. However, the answer below has been provided purely by assuming what the statement would have been.

CODE:

beta = 5;
do
{
    switch (beta)
    {
        case 1: cout << 'R'; break;
        case 2: cout << 'S'; break;
        case 4: cout << 'O'; break;
        case 5: cout << 'L';
    }
    beta--;
}while (beta>1);
cout << 'X';

OUTPUT:

LOSX

EXPLANATION:

Iteration 1 of do-while loop:
    Here, beta = 5 and case 5 of switch will run and 'L' will get printed on the console.
    Now, beta-- will cause beta to become beta = 4
    
Iteration 2 of do-while loop:
    Here, beta = 4 and case 4 of switch will run and 'O' will get printed on the console.
    Now, beta-- will cause beta to become beta = 3
    
Iteration 3 of do-while loop:
    Here, beta = 3 and there is no case 3 in switch. So nothing will be printed on the console this time.
    Now, beta-- will cause beta to become beta = 2
    
Iteration 4 of do-while loop:
    Here, beta = 2 and case 2 of switch will run and 'S' will get printed on the console.
    Now, beta-- will cause beta to become beta = 1
    
Now, loop condition will become false and program control will come out of the loop as beta = 1.

Finally, 'X' will be printed on the console.

LOSX is printed on the console 

FOR BETTER HELP PROVIDE THE MISSING STATEMENT AND PLEASE RATE THE ANSWER.

Add a comment
Know the answer?
Add Answer to:
C++ Program What is the output of the following code fragment?(beta is of type int.) beta...
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
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