Explain the conditional operator in C with the help of an example. Compare it with if...else statement.
THE CONDITIONAL OPERATOR IN C
The conditional operator works on three operands, so it is also known as the ternary operator. It is represented by two symbols, i.e., '?' and ':'.
If the first operand condition is true, the second operand represents the value entire conditional expression, if is false then the third operand represents the value.
The syntax is as follows:
(condition)? (expression1): (expression2);
If condition is true, expression1 is evaluated else expression2 is evaluated.
Let us see the following examples:
(i) x= (y<20) ? 9: 10;
This means, if (y<20), then x=9 else x=10;
Comments
Post a Comment