Conditional Operator

Conditional Operator:

Certain if…else statements can be written in a more concise way by using C++’s
conditional operator. The conditional operator, written as ?:, is a ternary operator,
which means that it takes three arguments. The syntax for using the conditional operator is:
expression1 ? expression2 : expression3
This type of statement is called a conditional expression. The conditional expression is
evaluated as follows: If expression1 evaluates to a nonzero integer (that is, to true), the
result of the conditional expression is expression2. Otherwise, the result of the conditional expression is expression3.
Consider the following statements:
if (a >= b)
max = a;
else
max = b;
You can use the conditional operator to simplify the writing of this if…else statement
as follows:
max = (a >= b) ? a : b;