Two conditions in one if statement does the second matter if the first is false?

前端 未结 6 1178
日久生厌
日久生厌 2020-12-04 23:52

Okay, so I have this piece of code tested and I found there isn\'t any exception thrown out.

public static void main(String[] args) {
    int[] list = {1,2}         


        
相关标签:
6条回答
  • 2020-12-05 00:22

    It is common for languages (Java and Python are among them) to evaluate the first argument of a logical AND and finish evaluation of the statement if the first argument is false. This is because:

    From The Order of Evaluation of Logic Operators,

    When Java evaluates the expression d = b && c;, it first checks whether b is true. Here b is false, so b && c must be false regardless of whether c is or is not true, so Java doesn't bother checking the value of c.

    This is known as short-circuit evaluation, and is also referred to in the Java docs.

    It is common to see list.count > 0 && list[0] == "Something" to check a list element, if it exists.


    It is also worth mentioning that if (list.length>2 && list[3] == 2) is not equal to the second case

    if (list.length>2){
        if (list[3] == 2){
            ...
        }
    }
    

    if there is an else afterwards. The else will apply only to the if statement to which it is attached.

    To demonstrate this gotcha:

    if (x.kind.equals("Human")) {
        if (x.name.equals("Jordan")) {
            System.out.println("Hello Jordan!");
        }
    } else {
        System.out.println("You are not a human!");
    }
    

    will work as expected, but

    if (x.kind.equals("Human") && x.name.equals("Jordan")) {
        System.out.println("Hello Jordan!");
    } else {
        System.out.println("You are not a human!");
    }
    

    will also tell any Human who isn't Jordan they are not human.

    0 讨论(0)
  • 2020-12-05 00:22

    Yes, both usages are semantically equivalent... That is, unless the length is > 2, then is the code allowed to access more than the 2nd element.

    In the first case, what matters is the && operator, think of it as: If first condition is true then apply second condition.

    The subsequent condition also has an implicit "then" clause

    if length greater than two
     then access third element
    
    0 讨论(0)
  • 2020-12-05 00:23

    You have specified in your IF statement that both of the conditions should be true for the System.out.println to be executed. So if the first condition itself fails,the second condition is not checked (this is for AND operator). But the second condition will be cheked if you have specified OR operator in the If statement.

    0 讨论(0)
  • 2020-12-05 00:25

    Right from the javadoc:

    The Conditional Operators

    The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.

    && Conditional-AND || Conditional-OR

    0 讨论(0)
  • 2020-12-05 00:30

    Yes. If the first condition is not satisfied then the remainder are not evaluated. This is known as short-circuiting. See here for more details. Note that this isn't particular to Java and lot of other languages will do the same.

    0 讨论(0)
  • 2020-12-05 00:30

    If first condition is false then it wont check for second condition as anything && F =F again it depends which bit wise operation you are doing . like in case of OR (||) if first condition is true (T) the it wont check for second condition as anything || T = T but if its false (F) then it will definitely check for second

    0 讨论(0)
提交回复
热议问题