Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

operator precedence

Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Hi,
How to evaluate a boolean expression having '&&' and '||' operators.
I know '&&' operator is having more precedence than '||'. In the folloing program first i applied '&&' operator, but the program's output is different. can any one of u explain me pls.
Deepthi Rallabandi
Greenhorn

Joined: Aug 17, 2005
Posts: 5
Hi,

In java always evaluation will be from left to right.

suppose take this expression

2 + 3 * 4 - 1 * 7

here you can think of the operands in expression will be grouped as follows

step 1: 2 + (3 * 4) - (1 * 7) according to precedence rules
step 2: ((2 + (3 * 4)) - (1 * 7)) accrding to associative rules

step 3: for any operator to be applied both operands must be first evaluated.
so as evaluation is from left to write first 2 + (3 * 4) needs to be evaluated.
2+(3*4)=> 2+12=14 then
14 - (1*7)=> 14 - 7 = 7

so final result is 7.

so in your code the grouping will be as follows
(a = true) || (b = true) && (c = true)
=> ((a=true) || ( (b=true) && (c=true) ))
as evaluation will be from left to write
(a=true) is evaluated first which results in true and "a" is assigned value true .
as || is short hand operator if left operand is true, right operand is not evaluated.

so output will be true, false , false as default values for b and c are false.
manogna edintipal
Ranch Hand

Joined: Aug 16, 2005
Posts: 51
The && and || are short circuit logical opeartors

these opearators first evaluates the left side expression (i.e., (a = true) || ) .If this is enough to evaluate the result of the whole expression these oparators didn't evaluate the expression which is in right side to the operator(i.e.,( b = true)&& (c = true)

the whole expression is evaluated from left to right

Sanju
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Thank You Deepthi

From this, i understood that, we must rewrite the given expression using (),based on the precedence of the operator.
Sheriff

Joined: Nov 24, 2005
Posts: 14688

16

|| is short hand operator if left operand is true, right operand is not evaluated.

That was the point ! Tricky question.

[My Blog]
Gagan Deep
Ranch Hand

Joined: Aug 23, 2005
Posts: 47
Nice Explanation Deepthi. Thanks....

Thanks
Gagan
Dmitryi Neverov
Greenhorn

Joined: Nov 28, 2005
Posts: 18
Originally posted by Deepthi Rallabandi:
Hi,

In java always evaluation will be from left to right.

suppose take this expression

2 + 3 * 4 - 1 * 7

here you can think of the operands in expression will be grouped as follows

step 1: 2 + (3 * 4) - (1 * 7) according to precedence rules
step 2: ((2 + (3 * 4)) - (1 * 7)) accrding to associative rules

step 3: for any operator to be applied both operands must be first evaluated.
so as evaluation is from left to write first 2 + (3 * 4) needs to be evaluated.
2+(3*4)=> 2+12=14 then
14 - (1*7)=> 14 - 7 = 7

so final result is 7.

so in your code the grouping will be as follows
(a = true) || (b = true) && (c = true)
=> ((a=true) || ( (b=true) && (c=true) ))
as evaluation will be from left to write
(a=true) is evaluated first which results in true and "a" is assigned value true .
as || is short hand operator if left operand is true, right operand is not evaluated.

so output will be true, false , false as default values for b and c are false.

Please explain why in KM book there is phrase
The evaluation order also respects any parentheses, and the precedence and associativity rules of operators.
Doesn't this means that operands of && operator must evaluated before operands of || , as && have higher precedence?
[ January 12, 2006: Message edited by: Dmitryi Neverov ]

SCJP - 98%<br />SCWCD - ...

I agree. Here's the link: http://aspose.com/file-tools

subject: operator precedence