conditional operator

 The conditional operator in the C programming language

I believe you would have found yourself in this situation one or more times, where you write an if-else code just to execute a single statement. That is kind of a fuss, right? The conditional operator was designed especially for this problem. The conditional operator can help you make decision-making statements in just a single line, whereas an if-else would take more than one line.

The conditional operator takes three operands, so it is a ternary operator. The conditional operator is the only ternary operator available in the C programming language, so the names ternary operator and conditional operator are used alternatively to mean the conditional operator. The operands can be expressions, statements, constants, or variables. Since they always start with a condition as the first operand it is suitably named as conditional operator.

The Operator ?: works as follows: Expression1 is evaluated first. If it is true, then the Expression2 is evaluated and becomes the value of the expression. If Expression1 is false, then Expression3 is evaluated and its value becomes the value of the expression.

Example : a = 500;

                    b = 700;

                    x = (a > b) ? a : b ;

                    Here x get the value of b because expression a > b is false.

                    x = b = 700




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.