How does the ternary operator work?

True : false; Boolean isValueBig; if( value > 100 ) { isValueBig = true; } else { isValueBig = false; }.

True : false; to make it even more boolean – i.o.w. This is a very pointless (though correct) use of the ternary operator. – peterchen Jan 21 '09 at 19:55 actually, in reality I'd hope you'd just use isValueBig = ( value > 100 ) ; it work the same :P – Kent Fredric Jan 22 '09 at 5:49.

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).

Thank you for your help – Sara S Jan 20 '09 at 23:31.

Then x else: y; (Notice that this isn't valid code. It's just what I trained myself to read in my head.).

Thank you for your good explanation. – Sara S Jan 20 '09 at 21:37.

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

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

Thank you so much – Sara S Jan 20 '09 at 21:22.

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.

Operator MSDN page, "the conditional operator (?:) returns one of two values depending on the value of a Boolean expression. " So you can use the ternary operator to return more than just booleans: string result = (value > 100 )?"value is big" : "value is small.

(expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE. " PHP Documentation on Comparison Operators.

Thank you Jonathan. – Sara S Jan 20 '09 at 21:25 You are welcome. I'm pleased to see how quickly you got responses - I love StackOverflow!

– Jonathan Sampson? Jan 20 '09 at 21:27.

Make sure you don't mix types in true/false parts in Java. It produces weird results :-(.

I agree, although explicit casts should handle it in most cases, but it looks so ugly. – WolfmanDragon Jan 21 '09 at 19:30.

True : false; as: bool isValueBig = value > 100 Beyond that, everyone else has already answered it. I would just not recommend using ternary operators to set bool values, since what you are evaluating is already a boolean value. I realize it was just an example, but it was worth pointing out.

Others have answered it already but here's one thing you should really know about ternary's usage and by that I mean don't ever do it. Lets assume that you have a piece of code which is supposed to return a different object for each possible variation of some value, lets say for simpliticy's sake an integer between 1 and 5. Your code looks like this: if(i==1) { return new ObjectOne(); } else if(i==2) { return new ObjectTwo(); } else if(i==3) { return new ObjectThree(); } else if(i==4) { return new ObjectFour(); } else if(i==5) { return new ObjectFive(); } else { return new DefaultObject(); } It's easy to understand but a bit heavy.

Since ternary is just another way of writing an if..else statement that can be refactored to this return (i==1)? New ObjectOne() : (i==2)? New ObjectTwo() : (i==3)?

New ObjectThree() : (i==4)? New ObjectFour() : (i==5)? New ObjectFive() : new DefaultObject(); It's called nested ternary.It's evil, now that you know about it please never use it.

It may seem to have its uses like the case above but it's very likely that in real life situations you would need to use it somewhere where it loses readability (think altering configurations with variable amount of parameters and such). Bonus sector: Never set attribute values inside if(), just look at this: if(bool=true! =false) { .. }.

Ternary is sugar anyway, and we know what sugar does to our teeth. Sugar is evil! – WolfmanDragon Jan 21 '09 at 20:26.

If yes, string result is "value is big", if no, string result is "value is small".

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions