if-else-if to switch-case – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-ICSE2018-03F #JAVA#4061    siteicon   siteicon  

Problem Statement - if-else-if to switch-case

Convert the following if else if construct into switch case
if( var==1)
System.out.println(“good”);
else if(var==2)
System.out.println(“better”);
else if(var==3)
System.out.println(“best”);
else
System.out.println(“invalid”);

Solution

TC++ #4061

The switch-case version will be as follows:
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”);
}

 


Share

sunmitra| Created: 25-Mar-2018 | Updated: 12-Apr-2019|ICSE2018









Back