Programming in "C‟ | Describe conditional operator with syntax and example.

Conditional operator is represented with ternary operator pair"?". It is uses to evaluate conditional expression. 

Syntax:     exp1? exp2 : exp 3 ; 

In the syntax , exp1, exp2 and exp3 are expressions. exp1 is evaluated first. If it is true, then expression exp2 is evaluated and resultant value of this expression is the output of conditional statement. If exp1 is false then exp3 is evaluated and its resultant value is the output of conditional statement.

Example:  int a=10,b=5,x;

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

In the above example x will take value 10 because given condition a>b is true. 


Post a Comment

0 Comments