How does the ternary operator work?


Solution 1

Boolean isValueBig = ( value > 100  ) ? true : false;

Boolean isValueBig;

if(  value > 100 ) { 
      isValueBig = true;
} else { 
     isValueBig = false;

Solution 2

The difference between the ternary operation and if/else is that the ternary expression is a statement that evaluates to a value, while if/else is not.

To use your example, changing from the use of a ternary expression to if/else you could use this statement:

Boolean isValueBig = null;
if(value > 100)
    isValueBig = true 
    isValueBig = false;

In this case, though, your statement is equivalent to this:

Boolean isValueBig = (value > 100);

Solution 3

When I was new to C++, I found that it helped to read this construct as follows:

Boolean isValueBig = if condition ? then x else: y;

(Notice that this isn't valid code. It's just what I trained myself to read in my head.)

Solution 4

I was never a fan of the ternary operator because I thought it was hard to read. As it so happens, Jon Skeet and his book, C# in Depth finally hit this old dog over the head and got it to sink in. Jon said, and I paraphrase, think of it as a question.

value > 100?

"yes" : "no"

Now the blind can see.

Hope this helps you make it second nature.

Solution 5

Boolean isValueBig;

if (value > 100)
   isValueBig = true;
   isValueBig = false;
Joefrey Ramos
Author by

Joefrey Ramos

Updated on June 04, 2022


  • Joefrey Ramos
    Joefrey Ramos 7 months

    Please demonstrate how the ternary operator works with a regular if/else block. Example:

    Boolean isValueBig = value > 100 ? true : false;

    Exact Duplicate: How do I use the ternary operator?

    • Joachim Sauer
      Joachim Sauer almost 14 years
      should "Java", "C" and a lot of other language be added as well? Since they all support the ternary operator pretty much the same way.
    • Andrew Hare
      Andrew Hare almost 14 years
      Not to be too picky but "(condition) ? :" is a conditional expression in the form of a ternary operator. It is in the form of a ternary operator because it takes 3 arguments.
    • abelenky
      abelenky almost 14 years
      This is valid use of the ternary operator, but a less-than-stellar-example, because the conditional-expression itself is boolean. Testing it to resolve to true/false is kinda redundant. This would be best written as: Boolean isValueBig = (value > 100);
    • Brian Rasmussen
      Brian Rasmussen almost 14 years
      That is a bad example of as it could more easily have been written as IsValueBig = value > 100; No need to assign bools based on an evaluation of a bool.
    • Dennis Kioko
      Dennis Kioko almost 14 years
    • Andrei Rînea
      Andrei Rînea almost 14 years
      I don't mean to be annoying (there's another word for that too) but wasn't there a rule that stated that you should try and document yourself BEFORE posting a question on Stackoverflow?!
    • Frederik Gheysels
      Frederik Gheysels almost 14 years
      I don't want to be annoying as well, but I believe that you could have gotten the answer to that question much quicker if you just looked in the help or msdn for the ternary operator.
    • WolfmanDragon
      WolfmanDragon almost 14 years
      I remember what my Java instructor told us. "Here is how you use a ternary operator .......... Now that you know what it is, don't EVER use it!" Sugar rots the best code.
    • user1568901
      user1568901 almost 14 years
      I can't imagine NOT using it. It's simple to read once you get used to it. Nice and short, more code stays on screen. Can insert where needed much easier. One of the best features of the C language family!
    • John Kraft
      John Kraft almost 14 years
      People who say not to use it do not understand it. These are, generally, the people that use it for flow control: (Condition) ? DoThis() : DoThat();
  • Sampson
    Sampson almost 14 years
    You are welcome. I'm pleased to see how quickly you got responses - I love StackOverflow!
  • WolfmanDragon
    WolfmanDragon almost 14 years
    I agree, although explicit casts should handle it in most cases, but it looks so ugly.
  • peterchen
    peterchen almost 14 years
    Or maybe Boolean isValueBig = (( value > 100 ) ? true : false) ? true : false; to make it even more boolean – i.o.w. this is a very pointless (though correct) use of the ternary operator.
  • WolfmanDragon
    WolfmanDragon almost 14 years
    Ternary is sugar anyway, and we know what sugar does to our teeth. Sugar is evil!
  • Kent Fredric
    Kent Fredric almost 14 years
    actually, in reality I'd hope you'd just use isValueBig = ( value > 100 ) ; it work the same :P
  • Zach Hutchins
    Zach Hutchins over 1 year
    "The ternary expression is a statement", the ternary expression is a expression, it expresses a value, if/else is a statement, ternary could become an expression statement but I would say by itself it is an expression, this is what makes turnery pretty powerful in my opinion.