• 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

Bizarre operators, please help me understand

 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 7 of the following code once is alien to anything I have ever seen:

Believe it or not, the output is:

Bear
Shark

Initially, luck = 10. The first if statement on line 7 is only supposed to print "Bear" if luck is greater than 10. That being said, what the hell is going on with line 7?!
What kind of operator is "?"?
What is up with "luck++:" (what is the colon doing there)?
What effect does the <10 have?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if ( (luck>10 ? luck++: --luck) <10 )

There's a few bits to this.

The first set of brackets is this:
luck>10 ? luck++: --luck
So, if luck is greater than 10 (it isn't) then post-increment luck, else pre-decrement luck.
Since luck is 10 then it is pre-decremented, so is 9.

(The pre and post are important, as they can affect the output).

Anyway, luck is now 9, so we get to the second bit of the if:
(9) < 10

Which is why you get into the if and print 'Bear'.

And finally, since luck is still 9, the second if statement passes as well.

As for the pre- and post- bit above, what happens if you do luck-- instead of --luck?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yes, I forgot to say what that statement meant:
luck > 10 ? luck++ : --luck

The bit before the '?' needs to be a boolean result.
The bit between the '?' and ':' is executed if it's true, and the bit after the ':' is executed if it's false.
The result is returned.  In this case the value of luck (and this is where the post- pre- bit comes into it, as they effect the value returned).
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The a ? b : c syntax denotes a single operator, also known as the conditional operator (or the ternary operator, because it is the only operator in Java that takes three operands). It first evaluates the boolean expression before the ?. If the expression evaluates to true, it evaluates the expression after the ?. Otherwise it evaluates the expression after the :.

It helps when we break the condition up in separate statements:

First we evaluate luck > 10. Because luck is 10, the condition is false and we evaluate the expression after the : which is --luck. So luck is decremented by one, and because we used the prefix decrement operator, the value 9 is assigned to the intermediateResult variable.

The intermediateResult is less than 10, so we print "Shark". After decrementing luck it is also less than 10, so we print "Bear".

This code was designed to be tricky. In general, don't use the ternary operator or the increment, decrement or assignment operators inside larger expressions.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic