Computer Science, asked by ariktaverma, 9 months ago


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

Answers

Answered by tamaghnadey1916
31

Explanation:

I think that this will help. If it helps you then please mark it as the brainliest

Attachments:
Answered by ridhimakh1219
23

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.

Similar questions