Unlike any other operator, conditional operator is one of the unique operator found in many programming languages.

These are some programming languages that support Conditional operator:
  • C
  • C++
  • Java
  • PHP
  • C# etc

This operator is used for evaluating a specific condition which eventually affects to choose any one of the two Boolean values or expressions. The outcome of the entire evaluation comes as either true or false. It is unique in the sense because it is a ternary operator and there is only one of such kind, which is this conditional operator.

It uses ?: for making the entire evaluation which has syntax like:

conditional_expression ? expression1 : expression2;

This above statement works something like this:

  • The 1st operand (here conditional expression), gets implicitly converted to Boolean (bool) type (either true or false).
  • When the 1st operand results to true, expression1 (which is the 2nd operand) gets evaluated, and
  • When the 1st operand results to false, expression2 (which is the 3rd operand) get evaluated.
Example C Program:

How internally it works:

int number = 8;
if (number % 2 == 0)
    printf("Divisible by two");
    printf("Not divisible by two");

The same statement with conditional operator:

int number = 8;

(num%2 == 0) ? printf("Divisible by two") : printf("Not divisible by two");

Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram