✏️ 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
💡

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