Menu
Question Index
...

Compare the following programs?

public class CompareIfSwitch1 {
    public static void main(String[] args) {
        int number = 109;
        if (number == 109) {
            System.out.println("1 Statement");
        } else if (number == +109) {
            System.out.println("2 Statement");
        } else if (number == 100) {
            System.out.println("3 Statement");
        }
    }
}
public class CompareIfSwitch2 {
    public static void main(String[] args) {
        int number = 109;
        switch (number) {
            case +109 :
                System.out.println("2 Statement");
            case 100 :
                System.out.println("3 Statement");
            case 109 :
                System.out.println("1 Statement");
        }
    }
}


Both CompareIfSwitch1 and CompareIfSwitch2 produce same output
CompareIfSwitch1 and CompareIfSwitch2 produce different output
CompareIfSwitch1 compiles and runs with out any error,
but CompareIfSwitch2 gives compilation error
CompareIfSwitch2 compiles and runs with out any error,
but CompareIfSwitch1 gives compilation error
Both CompareIfSwitch1 and CompareIfSwitch2 fail to execute.  

Doubts

Problems

Topic: if else Vs switch Performance In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App