Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a result of <a href="http://en.wikipedia.org/wiki/Short-circuit_evaluation" rel="nofollow noreferrer">short-circuit evaluation</a>.</p> <p>The expression <code>++x</code> evaluates to <code>2</code>, and the compiler knows that <code>2 || anything</code> always evaluates to <code>1</code> ("true") no matter what <code>anything</code> is. Therefore it does not proceed to evaluate <code>anything</code> and the values of <code>y</code> and <code>z</code> do not change.</p> <p>If you try with </p> <pre><code>x=-1; y=z=1; </code></pre> <p>You will see that <code>y</code> and <code>z</code> <em>will</em> be incremented, because the compiler has to evaluate the right hand side of the OR to determine the result of the expression.</p> <p><strong>Edit:</strong> asaerl answered your follow-up question in the comments first so I 'll just expand on his correct answer a little.</p> <p>Operator precedence determines how the parts that make up an expression bind together. Because AND has higher precedence than OR, the compiler knows that you wrote</p> <pre><code>++x || (++y &amp;&amp; ++z) </code></pre> <p>instead of</p> <pre><code>(++x || ++y) &amp;&amp; ++z </code></pre> <p>This leaves it tasked to do an OR between <code>++x</code> and <code>++y &amp;&amp; ++z</code>. At this point it would normally be free to select if it would "prefer" to evaluate one or the other expression first -- as per the standard -- and you would not normally be able to depend on the specific order. This order has nothing to do with operator precedence.</p> <p>However, specifically for <code>||</code> and <code>&amp;&amp;</code> the standard demands that evaluation will <em>always</em> proceed from left to right so that short-circuiting can work and developers can <em>depend</em> on the rhs expression not being evaluated if the result of evaluating the lhs tells.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload