How do I use the conditional operator in C/C++?


This conditional operator is also known as the Ternary Operator. This operator has three phase.

Exp1 ? Exp2 : Exp3;

where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon. The value of a ? expression is determined like this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.

The ? is called a ternary operator because it requires three operands and can be used to replace if-else statements, which have the following form

if(condition) {
   var = X;
} else {
     var = Y;
}

For example, consider the following code

if(y < 10) {
   var = 30;
} else {
  var = 40;
}

Above code can be rewritten like this

var = (y < 10) ? 30 : 40;

Example Code

#include <iostream>
using namespace std;
int main () {
   // Local variable declaration:
   int x, y = 10;
   x = (y < 10) ? 30 : 40;
   cout << "value of x: " << x << endl;
   return 0;
}

Output

value of x: 40

Updated on: 30-Jul-2019

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements