Introduction
Ternary operators return values based on the outcomes of relational expressions. For example, if you want to return the value of 1 if the expression is true and 2 if it is false, you can use the ternary operator.
Program/Example
If you want to assign the maximum values of i and j to k then you can write the statement
k = ( i>j ) ? i : j;
If i > j then k will get the value equal to i, otherwise it will get the value equal to j.
The general form of the ternary operator is:
(expr 1) ? expr2 : expr3
If expr1 returns true then the value of expr2 is returned as a result; otherwise the value of expr3 is returned.
No comments:
Post a Comment