Home / Questions / 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");
Explanatory Question

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");

👁 73 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation


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");
}