views:

1298

answers:

12

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?

+1  A: 

PHP Example

<?php

  // Example usage for: Ternary Operator
  $action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

  // The above is identical to this if/else statement
  if (empty($_POST['action'])) {
    $action = 'default';
  } else {
    $action = $_POST['action'];
  }

?>

"The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE."

PHP Documentation on Comparison Operators

Jonathan Sampson
thank you Jonathan.
Sara S
You are welcome. I'm pleased to see how quickly you got responses - I love StackOverflow!
Jonathan Sampson
+18  A: 
Boolean isValueBig = ( value > 100  ) ? true : false;


Boolean isValueBig;

if(  value > 100 ) { 
      isValueBig = true;
} else { 
     isValueBig = false;
}
Kent Fredric
thank you so much
Sara S
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.
peterchen
actually, in reality I'd hope you'd just use isValueBig = ( value > 100 ) ; it work the same :P
Kent Fredric
+4  A: 
Boolean isValueBig;

if(value > 100) { isValueBig = true; } else { isValueBig = false; }
thank you so much
Sara S
+5  A: 
Boolean isValueBig;

if (value > 100)
{
   isValueBig = true;
}
else 
{
   isValueBig = false;
}
empi
+6  A: 

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

Konrad Rudolph
thank you for your good explanation.
Sara S
+10  A: 

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);
Dan Monego
thank you for your help
Sara S
+2  A: 

As quoted from the ?: 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";
Craig
+4  A: 

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.

billb
+1  A: 

Bad example, because you could easily write

Boolean isValueBig = value > 100 ? 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.

Brian Rudolph
+1  A: 

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

Peter Štibraný
I agree, although explicit casts should handle it in most cases, but it looks so ugly.
WolfmanDragon
+1  A: 

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

Esko
Ternary is sugar anyway, and we know what sugar does to our teeth. Sugar is evil!
WolfmanDragon
A: 

As quoted from MSDN (noted in a previous post)

string result = (value > 100 ) ? "value is big" : "value is small";

Could be read as:

Is value greater than 100? If yes, string result is "value is big", if no, string result is "value is small".