Conditional Operators

Blog Contents

Conditional Operators:

Conditional operators also called ternary operators. Because it has three sets of expressions. Conditional operators return one value if the condition is true and returns another value if the condition is wrong.
The syntax of the expression and example is given below.

Syntax:

(Condition?true_value:false_value);

Example:

(a>b?a:b);
Let’s consider a program for the detailed understanding,
#include<stdio.h>
Int main()
{
Int a=10;
Int b=20;
Int c;
Printf(“The value after conditional operation is %d”,c=(a>b?a,b));
Return 0;
}

Output:

The value after conditional operation is 20
So in this program, We are initializing two integers a, b as 10 and 20 And another variable c. we are took
use of printf to print the output of conditional operator.
Conditional operator comparing the values of a and b. if a is greater than b. it will print the value of a or
b. In our example b is greater than a. so the conditional operators will print the variable b value.