Question

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?

Was it helpful?

Solution

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


Boolean isValueBig;

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

OTHER TIPS

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

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

Boolean isValueBig;

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

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

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.

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";

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

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

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.

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top