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
}
else
{
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;
}
else
{
isValueBig = false;
}
![Joefrey Ramos](https://i.stack.imgur.com/Aq6mJ.jpg?s=256&g=1)
Joefrey Ramos
Updated on June 04, 2022Comments
-
Joefrey Ramos about 2 years
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 over 15 yearsshould "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 over 15 yearsNot 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 over 15 yearsThis 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 over 15 yearsThat 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 over 15 yearsDuplicate of: stackoverflow.com/questions/392932/….
-
Andrei Rînea over 15 yearsI 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 over 15 yearsI 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 over 15 yearsI 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 over 15 yearsI 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 over 15 yearsPeople 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 over 15 yearsYou are welcome. I'm pleased to see how quickly you got responses - I love StackOverflow!
-
WolfmanDragon over 15 yearsI agree, although explicit casts should handle it in most cases, but it looks so ugly.
-
peterchen over 15 yearsOr 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 over 15 yearsTernary is sugar anyway, and we know what sugar does to our teeth. Sugar is evil!
-
Kent Fredric over 15 yearsactually, in reality I'd hope you'd just use isValueBig = ( value > 100 ) ; it work the same :P
-
Zach Hutchins almost 3 years"The ternary expression is a statement", the ternary expression is a
expression
, it expresses a value, if/else is astatement
, ternary could become anexpression statement
but I would say by itself it is an expression, this is what makes turnery pretty powerful in my opinion.