• 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

What is the value of " boolean x = (a=true) || (b = true) && (c = true);"

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value was true,flase,false..

Should this be boolean x = (a=true) || (b = true) && (c = true);

&& has more precedence over ||

So (a=true) || ((b=true) && (c=true))

= true || (true && true)

= true || true..

So value should be true,true,true..

Is n't this similar to 1 + 2 * 3 = 1 + (2 * 3) = 1 + 6 = 7

Please advise the rules of precedence

Thanks
Satya
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A hint: Does the term "short-cut" logical (or boolean) operator mean anything to you?
[ August 20, 2005: Message edited by: Barry Gaunt ]
 
satya mamillapalli
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know short-circuited evaluation..

I am getting confused with the order of precedence.

Should it finish && first and then ||???

Thanks
SAtya
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All that needs to be evaluated for this expression is a=true, so the evaluation of the rest of expression is short-circuited, leaving b and c at their previous values (assumed to be false).

I do not think that the complete question has been specified, so that it is not clear what is being printed. I am assuming it is a, b, c.
[ August 21, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Satya,
I think it should be

x = (a=true) || (b = true) && (c = true)
x = (a=true) || ( (b = true) && (c = true))
x = true || ( (b = true)&&(c = true) )
x = true

this is because evaluation will be from L to R

Hope this will help you!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic