This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes difference between ifs Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "difference between ifs" Watch "difference between ifs" New topic
Author

difference between ifs

Bruno Sant Ana
Greenhorn

Joined: May 17, 2012
Posts: 21
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
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
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4750
    
    7

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


Isn't it funny how there's always time and money enough to do it WRONG?
Bruno Sant Ana
Greenhorn

Joined: May 17, 2012
Posts: 21
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
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9948
    
    6

it has to do with both the precedence of operators and the difference between "||" and "|".


Never ascribe to malice that which can be adequately explained by stupidity.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: difference between ifs
 
Similar Threads
Vector passed by value or by referance???
Doubt In Kathy& Bates book questions at page 198
Looping constructs
how to compare the values in arrays
another 3 questions