0

Here is my problem: x is 11, y is 6 and z is 1.

what is the value of w?

w = x == y || x != y && z > x;

I dont understand how to decide whether I should evaluate the || or the && first.

  • 1
    See e.g. [this operator precedence table](http://en.cppreference.com/w/c/language/operator_precedence). – Some programmer dude Oct 25 '13 at 05:55
  • 5
    When in doubt, use parenthesis! – devnull Oct 25 '13 at 05:56
  • 1
    If in doubt, add parenthesis and you'l remove ambiguity. Anyway, in 2 weeks, you won't remember and have to recheck or read the helpful comment that you shall write. Not really worth the hassle compared to 4 key press to add parenthesis – Bruce Oct 25 '13 at 05:56
  • Unless you're about to make a submission to the [IOCCC](http://ioccc.org). – devnull Oct 25 '13 at 05:57
  • You might want to see **[this](http://stackoverflow.com/questions/19463670/why-printf-is-not-printing-here/19464112#19464112)** – smRaj Oct 25 '13 at 05:59
  • @smRaj - Your quote `While precedence may influence the order of evaluation, it doesn't determine the order of evaluation` is Wrong. Please read the answer here by Jerry - http://stackoverflow.com/questions/5473107/operator-precedence-vs-order-of-evaluation/5475260#5475260 – Sadique Oct 25 '13 at 06:12
  • @Acme: May you tell exactly in what sense it is wrong ? And, did you really read my whole answer? Especially `1+2*3` portion and if yes, Can you tell that precedence didn't influence the evaluation in this example ? – smRaj Oct 25 '13 at 07:10
  • You have taken individual values and not expressions hence the example does not clarify the point i am making. `While precedence may influence the order of evaluation` - Precedence does **not** have anything to do with Order of Evaluation - **no influence at all**. Order of evaluation does not depend on precedence, associativity, or (necessarily) on apparent dependencies. – Sadique Oct 25 '13 at 09:19
  • Even if you are not in doubt, use parenthesis, (or break it up and use intermediate booleans). Some other poor maintenance/enhancement engineer my have to fix/modify your code in the future. Needlessly complex boolean expressions that depend upon operator precedence are pretty much high-up in my list of 'what prat wrote this crap' table. – Martin James Oct 25 '13 at 09:32
  • @smRaj Think about this example ` (2 + 3) * (3+1)'.Here there are 2 expressions - `2+3` and `3+1` it depends on how the tree is traversed and how the expression tree is generated - precedence will **never** be able to dictate which one will be **evaluated** first. See the image here to understand what i mean - http://postimg.org/image/qbb01df13/ - How can you say now which one will be evaluated first? Its all dependent on implementation not precedence. – Sadique Oct 25 '13 at 09:32

3 Answers3

3

&& has higher precedence than ||. What else does one need to know?

x == y || x != y && z > x;

x != y evaluates to true, and z>x evaluates to false so x != y && z > x evaluates to false. So we have:

x == y || false;

Now since x==y is false we get:

false || false;

However order of evaluation does not depend on precedence if that is where you were confused. For a deeper understanding about what i said please read the answer here to my question by Jerry Coffin.

Operator Precedence vs Order of Evaluation

Community
  • 1
  • 1
Sadique
  • 22,572
  • 7
  • 65
  • 91
1

That would be evaluated like this w = (x == y) || (x != y && z > x) wikipedia has the full order for all operations http://en.wikipedia.org/wiki/Order_of_operations#Programming_languages

David Ewen
  • 3,632
  • 1
  • 19
  • 30
0

Take a look at http://www.isthe.com/chongo/tech/comp/c/c-precedence.html for the order of precedence. In this case && has higher precedence than ||.

In case you are not sure, just use bracket. Putting a bracket is much more useful rather than having such a headache.

rcs
  • 6,713
  • 12
  • 53
  • 75