Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/a/3700384/2455888">Unwind</a>, <a href="https://stackoverflow.com/a/3700391/2455888">R</a> and others have explained what really happens. So let me just add:</p> <p>The premise of your question is faulty. The fact that the <code>&amp;&amp;</code> has higher precedence doesn't mean that operands that surround it must be evaluated before any operands in the expression with lower precedence. Even where the special case short-circuiting of <code>||</code> and <code>&amp;&amp;</code> this wouldn't necessarily be so.</p> <p>For example, consider <code>a=b+c+d*e</code>; <code>*</code> has higher precedence than <code>+</code>, but that doesn't mean that <code>d*e</code> must be evaluated before <code>b+c</code>. It just means that it must be evaluated before we add the product to the expression as a whole. A compiler could evaluate this expression as <code>temp1=d*e</code>, <code>temp2=b+c</code>, <code>a=temp1+temp2</code> or it could evaluate <code>temp1=b+c</code>, <code>temp2=d*e</code>, <code>a=temp1+temp2</code>. Both would be equally valid.</p> <p>With the short-circuiting behavior of <code>||</code> and <code>&amp;&amp;</code>, there are some additional restrictions placed on order of evaluation.</p> <hr> <p>As a side note: In general I would avoid writing code like this. I can easily see another programmer trying to read this code getting confused about just when the increments will happen and when they won't. Well, maybe if you used real variable names it would not look so bizarre.</p> <p>I do occasionally rely on short-circuiting preventing side effects. Like</p> <pre><code>if (!eof() &amp;&amp; readNextInt()&gt;0) </code></pre> <p>I'm relying on the short-circuit to prevent reading if we're already at end of file, Or </p> <pre><code>if (confirmDelete==YES &amp;&amp; deleteEntry()!=-1) </code></pre> <p>I'm relying on the first test to short-circuit on false so I don't do the delete when I shouldn't. But these examples seem pretty straightforward to me, I'd hope any competent programmer would see what I'm doing. But when the examples get cryptic, I think it needs to be broken out. Consider </p> <pre><code>if (customerType==RETAIL || lastDepositAmount()&gt;100.00) </code></pre> <p>If <code>lastDepositAmount()</code> had a side effect, then if <code>customerType</code> is retail this side effect will never happen. I don't think that would necessarily be obvious to a reader. (Partly because the function name implies that it is retrieving data and not performing any update, and partly because there is no obvious relationship between the customer type and the amount of a deposit -- these sound like two independent things.) Admittedly, this is subjective. But when in doubt, choose simplicity and clarity over a trivial performance improvement. Always choose simplicity and clarity over <em>"hey this is a way cool use of an obscure feature, anyone reading this will be impressed at how smart I must be to understand the language well enough to do this"</em>.</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