| 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
|
|
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
|
|
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
|
|
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
|
|
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.
|
 |
 |
|
|
subject: difference between ifs
|
|
|