Ternary If-else operator

Ternary If-else operator


Posted in : Core Java Posted on : October 13, 2010 at 6:32 PM Comments : [ 0 ]

This section contains the detail about the Ternary If-else operator in java.

Ternary If-else operator

This is operator is slightly different because it has three operands. Unlike 'if-else' control statement, it produces value. Some of you are confused that it is if-else control statement but it is not using 'if' or 'else' keyword in coding. It's name is similar because it's implementation structure is similar to it. It's structure is of the  form :

boolean-exp ? value0 : value1

If boolean-exp evaluates to true, value0 is evaluated, and its result becomes the value produced by the operator. If boolean-exp is false, value1 is evaluated and its result becomes the value produced by the operator.

For example :

static int DemoTernary(int a) {
  return a < 30 ? a * 100 : a * 10;
} 

If the passed value the function

The conditional operator can be used for its side effects or for the value it produces, but in general you want the value, since that?s what makes the operator distinct from the if-else.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics