1

What's the checking prority of conditions inside if()?

For example in the below code which variable does the compiler start from to check?

On the other hand, if the b1 is true, do the compiler checks b2 and b3? Or the another one is true, the compiler checks others?

if((b1 && b2) || b3);
ATES
  • 281
  • 1
  • 11
  • 29
  • The variables are tested one after amother, from left to right(except something might get optimized away). – dan1st May 20 '20 at 20:39
  • 2
    That's not a valid statement - there are two opening parentheses and three closing ones – Player One May 20 '20 at 20:40
  • Update condition to be `if((b1 && b2) || b3);` remove the `)` – 0xh3xa May 20 '20 at 20:54
  • 2
    Does this answer your question? [Boolean expression order of evaluation in Java?](https://stackoverflow.com/questions/2028653/boolean-expression-order-of-evaluation-in-java) – Janez Kuhar May 20 '20 at 21:03
  • This doesn't have anything to do with `if`. It would be exactly the same for `result = ((b1 && b2)) || b3)`. – Charles Duffy May 20 '20 at 21:42

4 Answers4

2

You can check it by yourself:

public class ConditionCheck {
    public static void main(String[] args) {
        if ((first() && second()) || third()) {
            System.out.println("total true");
        }
    }

    private static boolean first() {
        System.out.println("first");
        return false;
    }

    private static boolean second() {
        System.out.println("second");
        return false;
    }

    private static boolean third() {
        System.out.println("third");
        return false;
    }
}

When:

  • first returns false
  • second returns false
  • third returns false

It outputs:

first third


When

  • first returns true
  • second returns false
  • third returns false

It outputs:

first second third


When

  • first returns true
  • second returns true
  • third returns false

It outputs:

first second total true


When:

  • first returns false
  • second returns false
  • third returns true

It outputs:

first third total true

And so on....

George Z.
  • 6,643
  • 4
  • 27
  • 47
1

The compiler will check the first expression first b1 && b2 then || b3

Because you defined && which is Short-circuits And will not check b2 if b1 is false, because whatever the value of b2 will give false

, And will check b2 if b1 is true

Check the table below:

1 AND 1 = 1
0 AND 1 = 0
1 AND 0 = 0
0 AND 0 = 0

, So we have output true or false from the first expression b1 && b2

if true no need to check the second expression b3 because of the Short-circuits OR

, but if the output false will check b3

1 OR 1 = 1
0 OR 1 = 1
1 OR 0 = 1
0 OR 0 = 0

Short-Circuit Logical Operator

0xh3xa
  • 4,801
  • 2
  • 14
  • 28
1

This is the java operator precedence (Operators at the same level have the same priority).

HIGHEST
---------------------------------------------------------------------
++(postfix) | --(postfix)   |       |       |           |           
++(prefix)  | --(prefix)    |   ~   |    !  |+(unary)   | -(unary)  
*           |       /       |   %   |       |           |           
+           |       -       |       |       |           |           
>>          |       >>>     |   <<  |       |           |           
>           |       >=      |   <   |   <=  | instanceof|           
==          |       !=      |       |       |           |           
&           |               |       |       |           |           
^           |               |       |       |           |           
|           |               |       |       |           |           
&&          |               |       |       |           |           
||          |               |       |       |           |           
?:          |               |       |       |           |           
->          |               |       |       |           |           
=           |       op=     |       |       |           |           
---------------------------------------------------------------------
LOWEST

Parentheses raise the precedence of the operations that are inside them. And then the expression is evaluated from left to right.

So here are the steps for your case:

1. (b1 && b2) (let's say the result is res)
2. res || b3

Let's say:

I:
b1, b2 and b3 = true
1. (true && true) = true
2. true || true = true
3. if(true)

II:
b1=false, b2=true, b3=true
1. (false && true) = false
2. if(false)
Since this result is false, the evaluation is shortcuited here.

III:
b1=true, b2=true, b3=false
1. (true && true) = true
2. true || false = true
3. if(true) 
sirandy
  • 1,834
  • 5
  • 27
  • 32
0

For &&, compiler goes to both the conditions and for || it doesn't go to the right side if the left is true.

In the given condition, the compiler would have to check b1 and b2, if the result is true then it wouldn't go to b3 otherwise it would.

Yudhishthir Singh
  • 2,941
  • 2
  • 23
  • 42