• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between ifs  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I don't understand what's the difference between the ifs bellow:



Why is the output like that?
it passed here - if 1
it passed here - if 4
it passed here - if 6
it passed here - if 7

For example, I thought if3 and if4 are the same. I thought if5 and if6 are the same as well, but they are not. Is it has to do with precedence of operators? What's the role's brackets?

Thanks
 
Marshal
Posts: 58448
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those statements are very different because the | operator has a higher precedence than && but || has a lower precedence than &&. No 3 and no 4 parse differently.
a && b | c means a && (b | c)
a && b || c means (a && b) || c
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bruno Sant Ana wrote:I don't understand what's the difference between the ifs bellow:


Your code looks remarkably similar to this Thread. Perhaps the discussion there might help.

Winston
 
Bruno Sant Ana
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wanted to confirm if it's related to precedence of operators. I found an URL with this subject and I'll take a look at it and do some tests:

http://bmanolov.free.fr/javaoperators.php

If appears more doubts I'll post here.

Thank for while
 
lowercase baba
Bartender
Posts: 12613
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it has to do with both the precedence of operators and the difference between "||" and "|".

 
Campbell Ritchie
Marshal
Posts: 58448
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That precedence table has two operators named incorrectly and one pair of operators in the wrong precedence level. you should ignore it.
The Java Tutorials has a table with the correct precedences, even though it calls && and || something different from here. “Conditional” is correct.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!