Concept explainers
Rewrite the following if-else-if statement as a switch statement.
if (selection == ‘A’)
System.out.println(“You selected A.”);
else if (selection == ‘B’)
System.out.println(“You selected B.”);
else if (selection == ‘C’)
System.out.println(“You selected C.”);
else if (selection == ‘D’)
System.out.println(“You selected D.”);
else
System.out.println(“Not good with letters, eh?”);
Want to see the full answer?
Check out a sample textbook solutionChapter 3 Solutions
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Additional Engineering Textbook Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Computer Systems: A Programmer's Perspective (3rd Edition)
Java How To Program (Early Objects)
Starting Out with Java: Early Objects (6th Edition)
Database Concepts (7th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
- Multi-way selection is possible with nested if-else statements and switch statements. When is it inappropriate to use a switch statement?arrow_forwardWrite an equivalent if...else if...else statement to replace the following switch statement int cents; switch (cents) { case 1: System.out.println("Cent."); break; case 5: System.out.println("Nickle."); break; case 10: System.out.println("Dime."); break; case 25: System.out.println("Quarter."); break; default: System.out.println("Not a valid value."); }arrow_forwardWrite a C++ program that : You have to do the following question only by nested if-statement. FROM GRADE A TO F A 93-100% 4.00 A- 90-92% 3.70 B+ 87-89% 3.30 B 83-86% 3.00 B- 80-82% 2.70 C+ 77-79% 2.30 C 73-76% 2.00 C- 70-72% 1.70 D+ 67-69% 1.30 D 63-66% 1.00 D- 60-62% 0.70 F Ask user to enter a grade and then determine the letter grade. Sample output: What is your grade: 94 Your grade on your trascript would be A.arrow_forward
- Using the following chart, write an if-else-if statement that assigns: S, M, L, depending on the weight: Weight Size Up to 120 lb S 120 lb to 175 lb M Over 175 lb Larrow_forwardDiscuss when you would choose to use multiple if statements versus a switch statement and vice versa. Explain your answer.arrow_forwardIn which cases you can transform an else-if statements to switch? Provide an example.arrow_forward
- Write an if-else statement with multiple branches. If givenYear is 2101 or greater, print "Distant future" (without quotes). Else, if givenYear is 2001 or greater (2001-2100), print "21st century". Else, if givenYear is 1901 or greater (1901-2000), print "20th century". Else (1900 or earlier), print "Long ago". End with a newline. Remember that "\n" in a string literal starts a new line. need this in Language Carrow_forwardQ- Write a program using an if statement instead of a switch statement. x = 2; Switch x case 1; Dis ("the one") case 2 de ("two") Case 3; dis ("three") otherwise; disp("unknown number") Endarrow_forwardWrite a switch statement that prints out the word: “paid” if the value of a variable called bill is ‘P’, “unpaid” if the value of bill is ‘U’, “late” if bill = ‘L’, “zero” if bill = ‘Z’.arrow_forward
- Convert the following C++ code from if-Statement to switch-statement, such that it gives the same output. int x;cin >> x;if (x == 1)cout << “EXCELLENT”;else if ((x == 2) || (x == 3) || (x == 4))cout << “GOOD”;elsecout << “BYE BYE”;arrow_forwardWhat is the difference between if and switch statement?arrow_forwardThe following control structures have one outcome, except ... Select one: a. char symbol = 'A' switch(symbol){ case A:System.out.println("Magnificent work!!"); case B:System.out.println("Excellent work!!"); break; case C:System.out.println("Good work!!"); break; default :System.out.println("Nice try!!"); break; } b. char symbol = 'A' if(symbol =='A' || symbol =='B'){ System.out.println("Magnificent work!!"); System.out.println("Excellent work!!"); }else if(symbol =='C'){ System.out.println("Good work!!"); }else{ System.out.println("Nice try!!"); } c. char symbol = 'A' if(symbol =='A'){ System.out.println("Magnificent work!!"); }else if (symbol =='B'){ System.out.println("Excellent work!!"); }else if(symbol =='C'){ System.out.println("Good work!!"); }else{ System.out.println("Nice try!!"); } d. All of the abovearrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education