Conditional Operator (?:):


Conditional operator (?:) is the only ternary operator available in C which operates on the three operands. The conditional operator together with operands forms a conditional expression which takes the following form:
                        exp 1 ? exp 2 : exp 3
Here, exp1 is a test condition which is evaluated first. If it is true then exp 2 is evaluated and this becomes the value of conditional expression. However if exp 1 is false then exp 3 is evaluated and this becomes the value of conditional expression. The value of the conditional expression can also be assigned to another variable. Note that only one of the expressions (either exp 2 or exp 3) is evaluated. For example, consider the following statements.
                        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 if…else statements as follows:
                        if (a > b)
                                    x = a;
                        else

                     &nb.sp;              x = b;

Previous                                                                                    Next


Powered by Blogger.