programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Rob Spoor
• Bear Bibeault
Saloon Keepers:
• Jesse Silverman
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Carey Brown
Bartenders:
• Piet Souris
• Al Hobbs
• salvin francis

# operator precedence

Ranch Hand
Posts: 163
• • Number of slices to send:
Optional 'thank-you' note:
• • 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.

Greenhorn
Posts: 5
• • Number of slices to send:
Optional 'thank-you' note:
• • 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.

Ranch Hand
Posts: 51
• • Number of slices to send:
Optional 'thank-you' note:
• • 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

Naresh Gunda
Ranch Hand
Posts: 163
• • Number of slices to send:
Optional 'thank-you' note:
• • Thank You Deepthi From this, i understood that, we must rewrite the given expression using (),based on the precedence of the operator.

Sheriff Posts: 14691
16   • • Number of slices to send:
Optional 'thank-you' note:
• • || is short hand operator if left operand is true, right operand is not evaluated.

That was the point ! Tricky question.

Ranch Hand
Posts: 47
• • Number of slices to send:
Optional 'thank-you' note:
• • Nice Explanation Deepthi. Thanks....

Thanks
Gagan

Greenhorn
Posts: 18
• • Number of slices to send:
Optional 'thank-you' note:
• • 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 ] You showed up just in time for the waffles! And this tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton