Conditional Operator


In C++, the ternary or conditional operator ( ? : ) is the shortest form of writing conditional statements. It can be used as an inline conditional statement in place of if-else to execute some conditional code. [1]

Syntax of conditional operator

expression1 ? expression2 : expression3

Where expression1 is evaluated and if TRUE, expression2 will be executed, if FLASE expression3 will be executed instead.

As an example:

int a = 3;
int b = 10;

int greater_val = (a > b) ? a : b; 

Where greater_val will be assigned the variable with the highest value.


  1. C++ Ternary or Conditional Operator - GeeksforGeeks ↩︎