Search code examples
coperator-precedence

Why does "++x || ++y && ++z" calculate "++x" first, even though operator "&&" has higher precedence than "||"


Why does ++x || ++y && ++z calculate ++x first, even though the precedence of operator && is higher than ||?


Solution

  • Unwind, R and others have explained what really happens. So let me just add:

    The premise of your question is faulty. The fact that the && 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 || and && this wouldn't necessarily be so.

    For example, consider a=b+c+d*e; * has higher precedence than +, but that doesn't mean that d*e must be evaluated before b+c. 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 temp1=d*e, temp2=b+c, a=temp1+temp2 or it could evaluate temp1=b+c, temp2=d*e, a=temp1+temp2. Both would be equally valid.

    With the short-circuiting behavior of || and &&, there are some additional restrictions placed on order of evaluation.


    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.

    I do occasionally rely on short-circuiting preventing side effects. Like

    if (!eof() && readNextInt()>0) 
    

    I'm relying on the short-circuit to prevent reading if we're already at end of file, Or

    if (confirmDelete==YES && deleteEntry()!=-1) 
    

    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

    if (customerType==RETAIL || lastDepositAmount()>100.00)
    

    If lastDepositAmount() had a side effect, then if customerType 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 "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".