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

Single Choice
Views 68

Answer:


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

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.