• Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operator precedence and associativity confusion........

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

i was just trying to understand how operatoe precedence and associativity works by going through this link which says well about it....according to me.

So i tried a question from K&B buk Chap 3 page 200(ques no. 12)

what is the result?
A. count = 0
B. count = 2
C. count = 3
D. count = 4
E. count = 10
F. count = 11


well everything goes fine but lin 18 i find some problem to evalute the result og the expression.....need help...
well after going through that link's content i see that logical OR(|) has higher precedence than conditional AND(&&).....right???
so i proceed like......if( b[0] && ( b[1] | b[2] ) )
which actually gives me wrong answer, according to the buk....it has been evaluated as if( (b[0] && b[1] ) | b[2] ) to get the given ans....

where does operator precedence comes to play here .....??
is it sth related to operator associativity???well can someone, with an illustration, bother to explain me this thing(associativity ....prcedence). I'll be really thankful to 'em......

one more doubt how is this evaluated by the compiler (i+++j+++++z)
assuming everythings properly initilized say to 0 and all are int primitives

anyone.......

thanx
amit
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if ( b[0] && b[1] | b[2] //How is this evaluated???)[



Hi Amit

logical OR(|) has higher precedence than conditional AND(&&).....right???

yes u r correct

When JVM interprets this line by seeing logical OR(|) it assumes left side of the OR operator as one set and right side of the operator as another set
so it evaluates in this fashion

( (b[0] && b[1] ) | b[2] )



one more doubt how is this evaluated by the compiler (i+++j+++++z)
assuming everythings properly initilized say to 0 and all are int primitives



whenever JVM see the first + operator it assumes ordinary unary plus,but if it see another + near by it assumes as increment operator.

so the code becomes

i++ + j++ + ++z

After that it will take it as unary plus(suppose if there is one more plus sign between j and i it throws error)

hope now U R clear
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

logical OR(|) has higher precedence than conditional AND(&&).....right???

yes u r correct

When JVM interprets this line by seeing logical OR(|) it assumes left side of the OR operator as one set and right side of the operator as another set
so it evaluates in this fashion

( (b[0] && b[1] ) | b[2] )



Hi Vidya ,
From your point of view if we are evaluating

a+ b * c
then JVM should interpret it as (a + b ) * c as * has higher precedence and + .
But it doesn't happen like this .
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vidya sagar:


When JVM interprets this line by seeing logical OR(|) it assumes left side of the OR operator as one set and right side of the operator as another set
so it evaluates in this fashion

( (b[0] && b[1] ) | b[2] )



That's actually not correct.

If that were true, then the left side of | would be evaluated, and then the right side of | would be evaluated, since | always evaluates both sides. However, this program demonstrates that that is not what is happening (see Case 4):


The output is:
a1=1, b1=0, c1=0, true, Case 1: true || true & false
a2=1, b2=0, c2=0, true, Case 2: true || (true & false)
a3=1, b3=0, c3=1, false, Case 3: (true || true) & false

a4=1, b4=0, c4=0, false, Case 4: false && true | true
a5=1, b5=0, c5=0, false, Case 5: false && (true | true)
a6=1, b6=0, c6=1, true, Case 6: (false && true) | true

The results of case 1 and case 2 are identical, and the results of case 4 and case 5 are identical. After a short-circuit operator, the remaining parts of the expression are not evaluated at all, as shown by the fact that the b and c variables in those cases were not incremented.

In case 4, there is an expression
++a4==9 && ++b4==1 | ++c4==1
in which neither ++b4==1 nor ++c4==1 gets evaluated, despite the fact that the | operator always evaluates both sides before returning a result. This shows that evaluation runs left-to-right until the && short-circuit operator determines that no more evaluation is necessary (because the left side of && was false). The rest of the expression is ignored, and false is returned.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi joe

In the case 1 why b value is not printing one,++ operatore has higher precedence than || (to my knowledge)

can u explain it???
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is that the rules of precedence do not apply the same way when using short-circuit operators ( && and || ).

In an expression with a short-circuit OR (||), if the left-hand side of the operator evaluates to true, then the right-hand side does not get evaluated.

In an expression with a short-circuit AND (&&), if the left-hand side of the operator evaluates to false, then the right-hand side does not get evaluated.

If a part of the expression does not get evaluated, then any side-effects of that part of the expression will not occur. This includes incrementing variables. Because the right-hand side of the expression does not get evaluated, the ++ operation never occurs.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic