• 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

A question about a line of code.

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


would somebody explain what is going on with this code. I am really unsure what is meant by it.

Is it the same as


Thanks
Tony

[edited due to a brief moment of clarity]
[ March 16, 2007: Message edited by: Tony VanHorn ]
 
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony,

Yepp - you got it in one.

The ternary operator (I think that is what it is called, due to it having three arguments) is in the form

 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony,

Yep, you got it, basically. Except that your code should maybe look like this...




...so you wouldn't be declaring a new variable y when you assign the value.


That compact bit of code is called a ternary operator. Here's an excerpt of what wikipedia's entry on it says:

...the ?: operator is used as a shorthand replacement for the if-then-else conditional statement; the general form is condition ? op1 : op2. If condition is true, the statement evaluates as op1; otherwise, it evaluates as op2.



The understandability/readability of the ternary operator has been the subject of a few discussions here, I'll try to dig one up...
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, you're quick Katrina! And I like your example way better than the one from wikipedia.

Here's a couple previous conversations about the ternary operator, interesting reading:


https://coderanch.com/t/4390/Cattle-Drive/ternary-operator
https://coderanch.com/t/3697/Cattle-Drive/Hint-Please

A search of the Cattle Drive on "ternary" brings up a few more too.
 
Tony VanHorn
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ladies. So this is ONLY used in assignment cases? I also just found this in the Just Java book as I was looking for something for the Grains assignment.

Tony
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony,

I don't think it has to be an assignment, as in you could probably do something like this:

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic