• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

If and parentheses

 
Ranch Hand
Posts: 63
IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Could someone tell me how parentheses are virtually set in this example? Is there a simple rule like 'if there are no parentheses, the first to checks build a pair'?
I mean, will z > 1 && y >6 be together or y > 6 || z == 2?




greez
Sam
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It all has to do with the precedence of the operators.

Java operator precedence
 
Sam Samson
Ranch Hand
Posts: 63
IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, that was quick

So in my example


z > 1 && y >6 is a pair

and in:

y > 6 | z == 2 would be a pair, right?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The easiest way to figure it all out is to use the operator precedence and start adding parentheses around the highest to lowest terms.

For example, if you started with 2 + 3 * 4, you'd end up with (2 + (3 * 4)). Give it a try.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And this is why you should always use parens. really, there is zero cost to adding them, and having them saves the next person reading it (which could be yourself) the trouble of having to remember the correct operator precedence.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:The easiest way to figure it all out is to use the operator precedence and start adding parentheses around the highest to lowest terms.



To add to that, if you encounter two operators with the same precedence, then you use the associativity. Most go from left to right, but some goes right to left.

Henry
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. I agree with fred -- for any but the most trivial of expressions, I use parens to make the meaning of the expression crystal clear.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Samson wrote:if(z > 1 && y > 6 | z == 2)


It may also be worth pointing out that a construct like y > 6 | z == 2 is extremely rare except when dealing with bit manipulation; it's almost always better to use '||'.

Winston
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the point are very valid, but personally I also find it handy to remember a small list so you don't get too surprised when you're reading other people's code. From highest to lowest priority:

1. Dereference (array[i] and obj.foo)
2. Unary (includes casts)
3. Arithmetic (includes shifts)
4. Comparison (includes instanceof)
5. Bitwise
6. Logical
7. Assignment

For instance, this is one I frequently bump my head into: (Something)something.getFoo(); when they mean: ((Something)something).getFoo(), because dereferences (. or []) have a higher precedence than casts.
 
reply
    Bookmark Topic Watch Topic
  • New Topic