switch (x) { case 1:System.out .println( “good”);break; case 2:System.out .println( “better”);break; case 3: System.out. println( “best”);break; default: System.out. println( “invalid”); }when x=5
Answers
Answered by
1
Answer:Convert the following if else if construct into switch case
Explanation:
By using switch statement instead of if else if construct
switch(var)
{
case 1:
System.out.println("good");
break;
case 2:
System.out.println("better");
break;
case 3:
System.out.println("best");
break;
default:
System.out.println("Invalid");
}
Note
If user need to choose one case among several choices, nested if-else can be used but when number of choices become large, then switch..case is considered as better option as program will be more easier to understand.
Explanation:
Similar questions
Science,
5 hours ago
Math,
5 hours ago
World Languages,
5 hours ago
World Languages,
10 hours ago
Math,
8 months ago