This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

short-circuit question  RSS feed

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I think,

1) a = true is evaluated, so a = true
2) b = true is not evaluated, so b = false (short-circuit)
3) the original expresion would be, boolean x = true && (c = true)
4) c = true is evaluated, so c = true
5) boolean x = true

But the answer is, a = true, b = false, c = false
Any ideas?
Thanks a lot.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Grady,

It actually works this way.

op1 || op2 where op1, op2 are two operands.

if op1 is "true" then op2 will not be evaluated, EVEN IF op2 is an expression (in this case (b=true) && (c=true)).
Similarly for &&, if op1 is false then op2 is not evaluated at all.

One more point to note is both ||, && have same level of precedence and so if they appear in a expression, they will be evaluated as usually (from left-right).

Say,


Here it will be evaluated as (false || (true && (true || false))) and so will print "check precedence".

Hope that was helpfull,
Kits
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Grady,

If you look at operator precedence order of || and &&, && has the higher precedence than operator ||.
And also if you remember java always evaluates left oprand first for binary operators.
So the expression will get evaluated as -

boolean x = (a = true) || (b = true) && (c = true);//Original expression.

x = ((a = true) || ((b = true) && (c = true)));//According to the precedence.

x = (true || ((b = true) && (c = true)));//Left oprand evaluated first.

At this point sencond expression i.e ((b = true) && (c = true)) will never get evaluated as has got short circuited.

Hence the result is,

x = true; with a = true , b = false & c = false.

I hope this answers your question and not adding any confusion
Correct me if my understanding is wrong!

Regards,
Kapil
 
Grady Jamyson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, guys.
I'm quite clear how it works after you two explained.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!