• 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

Short circuit operators

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Can anyone explain how does this work? Please let me know if there any good weblinks to get information about short circuit operators?


Thanks
Narayan

Edited by Corey McGlone: Added CODE Tags
[ May 28, 2004: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short Circuit operators take advantage of the rules of logic to streamline code. For example, if you have the expression:

false && someBoolean

You can tell simply by looking at the first part that this will always evaluate to false. The literal false "ANDed" with anything is always going to give false. Likewise, this statement:

true || someBoolean

will always result in a true result. It doesn't matter what someBoolean is, the literal true "ORd" with anything will result in a true result.

In your program, the JVM is taking advatange of that. Basically, it will begin evaluating operands from left to right until it can determine the final outcome and, once it does, it moves on without bothering to evaluate the remaining operands.

So, what does that mean in your example? Well, look at this line:

b = (t || ((i++) == 0));

The JVM starts with the left-most operand and evaluates it. It finds that t is true. Well, now we're going to OR t with some other operand. But hold on a second! We don't need to. We know that t is true and we know that true "ORd" with anything will result in a true result. Therefore, the JVM can skip the rest of that line and move to this line:

b = (f || ((i+=2) > 0));

In this case, f is false so we must go on to evaluate the second operand. That second operand has the side-effect of adding 2 to i before evaluating to true. That's why, when you execute this, you get a value of 2 printed.
 
deva raj
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great...Great.. I understood the concepts
Thanks alot
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic