短路OR和OR

时间:2018-02-05 16:58:59

标签: java if-statement

我仍然难以理解OR和短路OR之间的区别 这是代码

package if_else_if;

public class if_Else_if {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int month = 4;
        String season;

        if(month == 12 || month == 1 || month == 2)
            season = "Winter";
        else if(month == 3 || month == 4 || month == 5)
            season = "Spring";
        else if (month == 6 || month == 7 || month == 8)
            season = "Summer";
        else if (month == 9 || month == 10 || month == 11)
            season = "Autumn";
        else 
            season = "Bogus month";

        System.out.println("April is a " + season + " month");
    }

}

如果我只是简单地说'或''代替短路OR'。我得到了同样的答案。 是否是执行时间产生差异。当我们使用' OR'时,它会评估所有条件,而“'短路OR'在获得&= 39;月== 4'

后跳过后续条件

0 个答案:

没有答案