The operator ?: is known as conditional operator.
Simple conditional operations can be carried out with conditional operator. An
expression that make use of the conditional operator is called a conditional
expression. Such an expression can be written in place of traditional ifelse statement.
A conditional expression is written in the form:
expression1 ?
expression2 : expression3
When evaluating a conditional expression, expression1
is evaluated first. If expression1 is true, the value of expression2 is the
value of conditional expression. If expression1 is false, the value of
expression3 is the value of conditional expression.
For example:
a = 10 ;
b = 15 ;
x = (a > b) ? a : b ;
In this example, x will be assigned the value of b.
This can be achieved by using the if_else statement as follows:
if (a<b)
x = a ;
else
x = b ;
Comments
Post a Comment