+ 1
what are ternary operators?
4 Answers
+ 4
ternary operators are conditional statements.
Example:
int a=2;b=3;
int c=a>b?10:20;
Since, value of a is smaller than b, so c will get 20.
If a was greater than b, then c would get 10.
Syntax:
<condition>?<true value>:<false value>;
+ 3
unary - single
binary - two
ternary - three
So ternary operator is an operator which has three operands.
The character pair ? : is a ternary operator.
operand1 ? operand2 : operand3
Here operand1 should be an expression/decision statement, operand 2,3 can be expressions or constants.
It is similar to this if else statement
if(operand1)
variable = operand2;
else
variable = operand3;
+ 1
A ternary operator has 3 operands.
There is only one ternary operator ie Conditional operator.
+ 1
This operator is like if condition, if term before ? is true then first condition executed otherwise second.
e.g. int a=2,b=3;
b>a?yes:no
Ans is "yes".