Conditional or Ternary Operator

The conditional operator is kind of similar to the if-else statement as it does follow the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.
Syntax: 
The conditional operator is of the form  
variable = Expression1 ? Expression2 : Expression3
It can be visualized into if-else statement as:  
if(Expression1)
{
    variable = Expression2;
}
else
{
    variable = Expression3;
}
Since the Conditional Operator ‘?:’ takes three operands to work, hence they are also called ternary operators.
Working: 
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then Expression2 will be executed and the result will be returned. Otherwise, if the condition(Expression1) is false then Expression3 will be executed and the result will be returned.
Posted on by