# Operator precedence &&, ||

Anonymous
Ranch Hand
Posts: 18944
Hi Friends,
In Applied Reasoning mock exam, the following question appeared:
i=3;
a=0;
if (i == 3 | | ((a=i) > 2)
System.out.println("value of a : "+ a);
what will be the value of a ?
my answer was 3. But according to the exam it is 0. I ran the code, i got the answer as 0.
Yes, i know the rule about shortcut operators. But the evaluation order in any expression for | | is lower compared to (), > is it not ?
If so then, the expression inside brackets should have evaluated first and then > and finally | |.
Please clarify.
cheers
sankar

Tom P
Ranch Hand
Posts: 88
The | | means evaluate the expression on the left and if it is true do nothing with the statement on the right. If it is false then evalute the statment on the right. Since the statement on the left evalutes to true, the if will be true and there is no reason to evaluate the statement on the left.
If the statement had been:
if (i == 3 | ((a=i) > 2)
then a would be equal to 3.

Anonymous
Ranch Hand
Posts: 18944
Hi Tom,
I agree with your point. My question is why do we say | | has a lower priority than (), all arithmatic operators and relational operators ?
Because as per your clarification (and IMO as well), the short-cut logical OR operator '| |' is always evaluated before any operators on the R:H.S.
IMO, then we should not have | |(also &&) should not be listed in the evaluation hierarchy.
If my conclusion is wrong, please clarify with an example where the shortcut operators (| |, &&) take lesser priority compared to arithmatic and relational operators appearing on the R.H.S of | |.
Thanks in advance for your time and effort.
cheers
sankar

Ajith Kallambella
Sheriff
Posts: 5782
Shankar,
Here is an example. Though not perfect, it will convince you about the precedence. I will not deliberate much, just try to reason out what's happening here. Ofcourse, as always if you are unclear, ask!
<PRE>
class Priority
{
public static void main( String[] s)
{
int a = 3 ;
System.out.println( a < 4 && 2+3 > 0 && 5+2 > 1 ) ;
}
}</PRE>
Ajith

Anonymous
Ranch Hand
Posts: 18944
one more example...
import java.lang.*;
public class test1
{
public static void main( String args[] ) {
int i=3;
int a=0;
if (i == 3 | | ( (a=i) > 2) ) {
System.out.println("value of a : "+ a);
int b = 3 ;
System.out.println( b < 4 && 2+3 > 0 ) ;
} // end of if
} // end of main
} // end of class
1. in the 1st if statement. the order of evaluation review the left poriton of the
short circuit OR first. then, the short circuit OR is evaluated. followed by
any statements after the short circuit OR. the point being. for both && and | |,
the statement to the left are evaluated before operator precedence is applied.
since the left operator is TRUE. the operator to the right of | | is never evaluated. the operator to the right of && will always be evaluated.
2. ie.
b is three
b is less than 4
therefore the first statment is TRUE
2+3 is 5
5 is greater than 0
therefore the second statemend is TURH
the answer is TRUE
Hope this clears things up.
Monty
[This message has been edited by monty6 (edited May 26, 2000).]
[This message has been edited by monty6 (edited May 26, 2000).]
[This message has been edited by monty6 (edited May 26, 2000).]

 Don't get me started about those stupid light bulbs.