Question

Nested if-else statements and switch statements both allow for multi-way selection. When would it not be...

Nested if-else statements and switch statements both allow for multi-way selection. When would it not be advisable to use a switch statement?

Homework Answers

Answer #1

The expression used in the switch must be integral type (int, char and enum). Any other type of expression is not allowed. So, when we want to use non-integral type data then we should go for nested if else.

Also, the switch is based on the value of a variable. If switch is based on a complex AND/OR/NOT boolean expression that varies for each condition, then we should go for nested if else.

PLEASE LIKE THE SOLUTION :))

IF YOU HAVE ANY DOUBTS PLEASE MENTION IN THE COMMENT

Know the answer?
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
Similar Questions
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output...
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below. if (selection == 10) System.out.println("You selected 10."); else if (selection == 20) System.out.println("You selected 20.") ; else if (selection == 30) System.out.println("You selected 30."); else if (selection == 40) System.out.println("You selected 40."); else System.out.println("Not good with numbers, eh?");
e-write following if-else-if statements as Switch statement. Your final code should result in the same output...
e-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below. if (selection == 10) System.out.println("You selected 10."); else if (selection == 20) System.out.println("You selected 20."); else if (selection == 30) System.out.println("You selected 30."); else if (selection == 40) System.out.println("You selected 40."); else System.out.println("Not good with numbers, eh?"); (Java programming)
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output...
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below. if (selection == 10) System.out.println("You selected 10."); else if (selection == 20) System.out.println("You selected 20."); else if (selection == 30) System.out.println("You selected 30."); else if (selection == 40) System.out.println("You selected 40."); else System.out.println("Not good with numbers, eh?"); Second question Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same...
Q1: Re-write following if-else-if statements as Switch statement. Your final code should result in the same...
Q1: Re-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below. if (selection == 10) System.out.println("You selected 10."); else if (selection == 20) System.out.println("You selected 20."); else if (selection == 30) System.out.println("You selected 30."); else if (selection == 40) System.out.println("You selected 40."); else System.out.println("Not good with numbers, eh?"); Q2: Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same...
1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that...
1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that will provide the same function. Char grade; String tstmsg; if (grade == ‘A’) {   tstmsg = “Excellent”; } else if (grade == ‘B’) {   tstmsg = “Good”; } else if (grade == ‘C’) {   tstmsg = “OK”; } else {   tstmsg = “Study More”; } 2.Write the following for statement as a while statement. for (k = 0; k < 3; k++) {   System.out.println...
Use a Switch statement.A switch statement is just another way of writing an IF statement. The...
Use a Switch statement.A switch statement is just another way of writing an IF statement. The user will enter f, s, j, or r from the keyboard for freshman, sophomore, junior, or senior. Use character data type for c++. Then you can say something like: char level ='f'; level=toupper(level); You would need a loop for 1 to 5, to test for f, s, j, r and also an illegal grade level. Use a "z" for the illegal grade level Convert...
   QUESTION 6 1.   If we wanted something like the python else statement for a counter...
   QUESTION 6 1.   If we wanted something like the python else statement for a counter controlled loop in Java, what would it look like?        Since there is no way for a loop in Java to tell you if it completed without breaks or continues, you'd need to set a boolean flag to indicate that and check it after the loop was completed.        We simply check either break or continue flags to see if either is...
When counseling employees what would be the correct and proper way to correct the following statements:...
When counseling employees what would be the correct and proper way to correct the following statements: a) Your laziness is becoming a problem. b) Stop shouting. You have no right to be angry; just look at your performance. c) There would be no problems if you took your work more seriously. Explin your answers. Question 2 options:
Discuss Single Cycle Processors and Multi-Cycle Processors: Expand on the pro-cons of each, when would you...
Discuss Single Cycle Processors and Multi-Cycle Processors: Expand on the pro-cons of each, when would you use one type, when the other and why.
If and else statements: 1a.) Given the following declaration: int temperature; Write an if statement that...
If and else statements: 1a.) Given the following declaration: int temperature; Write an if statement that prints the message "The number is valid" if the variable temperature is within the range of -50 trough 150 (both values excluded). 1b.) Given the following declarations int x; int y; Write an if statement that assigns 100 to x when y is equal to 0. 1c.) Given the following declaration: int grade; Write an if statement that prints the message "The number is...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT