Program Flow  «Prev 

Shorthand if-else Statement using ternary Operator

Conditional branching basics

The conditional operator[1] functions as a shorthand if-else statement.
This operator
( ?: ) 

evaluates a test expression[2] and conditionally evaluates another expression based on the test.

Ternary construct

You can use a ternary operator, ?:, to define a ternary construct. A ternary construct can be compared to a compact if-else construct, used to assign a value to a variable depending on a boolean expression.
In the following example, the variable discount is assigned the value 15 if the expression (bill > 2000) evaluates to true but 10 otherwise:

int bill = 2000;
int discount = (bill > 2000)? 15 : 10;
System.out.println(discount);

Figure 5.2 compares a ternary construct used in the preceding example with an if-else construct.
Ternary Construct
int bill = 2000;
int discount = (bill > 2000)? 15 : 10;


if-else construct
int bill = 2000;
int discount
if (bill > 2000)
 discount = 15;
else
 discount = 10;

The parentheses enclosing a boolean expression are optional and are used for better readability. The code will work without them
// boolean expression not enclosed within ()
int bill = 2000;
int discount = bill > 2000? 15 : 10;

The variable discount might or might not be declared in the same statement that includes the ternary operator:
int bill = 2000;
int discount;
discount = (bill > 2000)? 15 : 10;

You can also assign an expression to the variable discount using a ternary operator. Here is the modified code:
int bill = 2000;
int discount = (bill > 2000)? bill-150 : bill - 100;
System.out.println(discount);

Conditional Operator is a Condensed Operator

The conditional operator is a condensed, limited form of a if statement. It is condensed in that the decision is limited to a single expression. It is limited because multiple statements cannot be included in the then or else clauses. It is sometimes called the ternary operator due to its three components. The essential form of the operator is as follows:

LogicalExpression ? ThenExpression : ElseExpression
If the LogicalExpression evaluates to true, then the result of the ThenExpression is returned. Otherwise the result of the ElseExpression is returned. The following simple example tests to see if a number is less than 10. If it is, 1 is returned, otherwise 2 is returned. The then and else expressions in the example are trivial integer literals.
result = (num < 10) ? 1 : 2;

This is equivalent to the following if statement:
if (num < 10) {
result = 1;
} else {
result = 2;
}



Relational Numeric Operator

In this example, a relational numeric operator is used to see if the myScore variable is less than the yourScore variable. If so, the message variable is set to "You win!"; otherwise it is set to "I win!".
Let us now reconsider the if-else example code shown in the

if (tired)
 timeForBed = true;
else
 timeForBed = false;

You could shorten this code using the conditional Java ternary operator as follows:
timeForBed = tired ? true : false;

To simplify things even more, you could shorten the code to this:
timeForBed = tired;

Quiz Question regarding Ternary Operators

Given the following code :
1. public class Example{
2.  public static void main(String[] args) {
3.   int x = 10;
4.   int i = 2; 
5. 
6.   boolean a = x > 10 ? true: false;
7.   boolean b = a=true ? ++i > 2 ? true:false:false;
8.   System.out.print(b);
9.  }
10.} 


What is the output?
  1. true
  2. false
  3. Compilation fails due to an error on line 8
  4. Compilation fails due to an error on line 7


Answer: A
Explanation:
Option A is the correct answer. In this code we have used two ternary operators. The ternary operator at line 7, can be read as follows and it will assign false to the variable "a".
"If x>10 is true, assign 'true' to boolean 'a', otherwise, assign 'false' to boolean 'a'."
The ternary operator at line 8 will assign true to the variable "a". As we used "a=true" there, the value of a will be changed to true and the
++i > 2
will be evaluated since it also true and true will be assigned to variable b.
Option A is correct as the value of the boolean "b" is true.

Oracle Reference
[1] Operator: An operator is a programming construct that performs an evaluation or computation on a data object or objects.
[2] Expression: An expression usually involves an equal sign