title: Order of Operations with Logic in Java Confused
body: <p>I have an order of operations problem from a quiz and the explanation is not exactly helpful. Here is the code:</p>
  
  <pre><code>package com.udayan.oca;
  
  public class Test {
       public static void main(String [] args) {
           int a = 2;
           boolean res = false;
           res = a++ == 2 || --a == 2 &amp;&amp; --a == 2;
           System.out.println(a);
       }
  }
  </code></pre>
  
  <p>It says it prints 3 which it does because I tested that but I do not understand how. Here is their explanation:</p>
  
  <pre><code>a++ == 2 || --a == 2 &amp;&amp; --a == 2;
  </code></pre>
  
  <p>[Given expression]. <code>(a++) == 2 || --a == 2 &amp;&amp; --a == 2;</code> </p>
  
  <p>[Postfix has got higher precedence than other operators]. </p>
  
  <pre><code>(a++) == 2 || (--a) == 2 &amp;&amp; (--a) == 2;
  </code></pre>
  
  <p>[After postfix, precedence is given to prefix]. </p>
  
  <pre><code>((a++) == 2) || ((--a) == 2) &amp;&amp; ((--a) == 2);
  </code></pre>
  
  <p>[== has higher precedence over &amp;&amp; and ||]. </p>
  
  <pre><code>((a++) == 2) || (((--a) == 2) &amp;&amp; ((--a) == 2));
  </code></pre>
  
  <p>[&amp;&amp; has higher precedence over ||]. </p>
  
  <p>Let's start solving it: <code>((a++) == 2) || (((--a) == 2) &amp;&amp; ((--a) == 2));</code></p>
  
  <p>[a=2, res=false].</p>
  
  <pre><code>(2 == 2) || (((--a) == 2) &amp;&amp; ((--a) == 2));
  </code></pre>
  
  <p>[a=3, res=false]. <code>true || (((--a) == 2) &amp;&amp; ((--a) == 2));</code></p>
  
  <p>[a=3, res=false].</p>
  
  <p>|| is a short-circuit operator, hence no need to evaluate expression on the right. </p>
  
  <p>res is true and a is 3. </p>
  
  <p>Yes, I understand short circuiting by the way so no need to explain that.</p>
  
  <p>So here is my thinking however:</p>
  
  <pre><code>res = a++ == 2 || --a == 2 &amp;&amp; --a == 2 -&gt;
  (((a++) == 2) || (((--a) == 2) &amp;&amp; ((--a) == 2))) [a = 2]
  
  (((a++) == 2) || ((**1** == 2) &amp;&amp; ((--a) == 2))) [a = 1]
  
  (((a++) == 2) || (**false** &amp;&amp; (**0** == 2))) [a = 1] //short-circuits
  
  (((a++) == 2) || **false**) [a = 1] //short circuits
  
  (**false**) [a = 1]
  </code></pre>
  
  <p>????
  Another point is that the answer key says to do a++ first then || next. a++ yah that makes sense. But I thought &amp;&amp; is before ||. </p>