Introduction to C Ternary Operator
C ternary operator is also referred as conditional operator or trinary operator because:
- It is only operator that has three operands.
- It is a shorthand of combination of the if-else and return statement.
The syntax of C ternary operator is as follows:
C first evaluates the condition. Based on the return value of the condition the second or third operand is evaluated:
- If the condition is evaluated to true (1), only the second
operand (expresion 1) is evaluated. Then the operator return the value
of the expression 1.
- If the condtion is evaluated to false (0), only the third
operand (expression 2) is evaluated. The value of the expression 2 is
returned.
The ternary operator can be rewritten as the if-else and return statement as follows:
0 comments:
Post a Comment
Dont Forget for Commets