In C++, ternary operator allows executing different code depending on the value of a condition, and the result of the expression is the result of the executed code. The ternary operator uses 3 operands. It evaluates a condition and after that chooses one of its two branches to execute, depending upon the result of condition. The symbol for ternary operator is “? :”. The syntax for the ternary operator is:

**Exp1 ? Exp2 : Exp3**

In the above C++ syntax, Exp1 is condition (a Boolean expression, which can either be true or false) while Exp2 and Exp3 can either be variable or numeric value or any mathematical expression or statement. If the condition would be true, Exp2 will be executed else Exp3 will be executed.

<condition> ? <true-case> : <false-case>;

Ternary operator can be considered as the replacement of if-else statements in C++. The other name for the ternary operator is conditional operator, that is perhaps more meaningful, as it evaluates the conditions just like if. Moreover, the ternary operator makes the code more concise without compromising readability.

Before getting to the examples of ternary operator, consider the following. All conditional expressions possess right-to-left associativity where the 1st operand of the expression must be of integer or pointer type. While the following rules are applicable to 2nd and 3rd third operands:

- If both 2
^{nd}and the 3^{rd}operands are of same type, the result will also be of that type. - If 2
^{nd}and 3^{rd}operands are of enumeration or arithmetic types, then the arithmetic conversions will be performed to transform them to a common type. - If 2
^{nd}and 3^{rd}both operands are of pointer type or if anyone one of them is of pointer type and the other one is constant expression, evaluates to 0, then simply pointer conversions will be performed to convert both of them to a common type. - If 2
^{nd}and 3^{rd}both operands are of reference types then of course reference conversions will be performed to convert both operands to a common type. - If 2
^{nd}and 3^{rd}both operands are of type void, then the common type will be void. - If both operands have user-defined type, the common type is that type.
- If both operands are of different types and out of them one has user-defined type, the language rules will be used to convert them to a common type.

Any combinations of 2^{nd} and 3^{rd} operands that are not mentioned in the above list are illegal.

Let’s consider some example to get hands-on knowledge of ternary operator.

## Find the largest number between two numbers using if-else statements

The following C++ code example demonstrates finding the largest number among two numbers using if-else statements.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> using namespace std; int main() { int a, b; cout << "Enter any two numbers" << endl; cin >> a >> b; if (a > b) { cout << a << " is largest number of given numbers " << endl; } else { cout << b << " is largest number of given numbers " << a; } return 0; } |

**Output:**

1 2 |
Enter any two numbers 45 33 45 is the largest number of given numbers |

## Find the largest number between two numbers using ternary operator

The following C++ code example demonstrates finding the largest number among two numbers using ternary operator.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> using namespace std; int main() { int a, b, max; cout << "Enter any two numbers" << endl; cin >> a >> b; cout << a << " " << b << endl; //the following statement replaces the whole if-else //statement and makes the code more concise max = (a > b) ? a : b; cout << max << " is the largest number of given numbers"; return 0; } |

**Output:**

1 2 3 4 5 |
Enter any two numbers 45 33 45 is the largest number of given numbers |

Here in above mentioned C++ example, you can see that the output of both codes is same, but the way they are written is different. The second code written by using ternary operator is more concise as compared to first one that is written using if-else statements.

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

The above mentioned c++ statement creates all the difference. The condition is *(a > b)*, that means, if *a* will be greater than *b*, value of *a* will become equivalent to whole expression and assigned to variable m*ax* but if it is not the case, the value of *a* is not greater than* b* then the whole expression will become equivalent to value of *b *and its value will be assigned to variable *max.*

Now let’s try a bit complex example of ternary operator:

## Find the largest number of three numbers using if-else statement

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> using namespace std; int main() { int a, b, c, max; cout << "Enter any three numbers" << endl; cin >> a >> b >> c; cout << a << " " << b << " " << c << endl; if (a > b > c) { cout << a << " is largest of given three numbers" << endl; } else if (b > a > c) { cout << c << " is largest of given three numbers" << endl; } else { cout << c << " is largest of given three numbers" << endl; } return 0; } |

**Output:**

1 2 3 4 5 |
Enter any three numbers 4 5 6 6 is largest of given three numbers |

The above mentioned C++ example compared three numbers using if-else statements and then outputs the largest number.

## Find the largest number of three numbers using nested Ternary Operator

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> using namespace std; int main() { int a, b, c, max; cout << "Enter any two numbers" << endl; cin >> a >> b >> c; cout << a << " " << b << " " << c << endl; //the following statement replaces the whole if-else //statement and makes the code more concise max = (a > b) ? (a > c ? a : c) : (b > c ? b : c); cout << max << " is the largest number of given three numbers"; return 0; } |

**Output:**

1 2 3 |
Enter any two numbers 4 5 6 6 is the largest number of given three numbers |

In above C++ example, 3 numbers are compared using ternary operator.

max = (a > b) ? (a > c ? a : c) : (b > c ? b : c);

In this C++ statement, nested ternary operators are used. *(a > b)* compares the value of *a *and*, *if the value of *a *is greater than value of b, *(a > c ? a : c) *will be executed that further compares the value of *a* with the value of *c, *if the value of *a* is greater than the value of *c, *the whole expression will become equivalent to the value of *a* and assigned to* max. *But if the value of* a* is smaller than the value of *c *the whole expression will become equivalent to the value of *a *and assigned to* max.*

And if *(a > b)* becomes false, then *(b > c ? b : c) *will be executed that further compares the value of *b* with the value of *c, *if the value of *b* is greater than the value of *c, *the whole expression will become equivalent to the value of *b* and assigned to* max. *But if the value of* b* is smaller than the value of *c *the whole expression will become equivalent to the value of *b* and assigned to* max.*

Let’s consider another C++ example to make your concepts more clear.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> using namespace std; int main() { int a, b, c; cout << "Enter any three numbers" << endl; cin >> a >> b >> c; cout << a << " " << b << " " << c << endl; //the following statement replaces the whole if-else //statement and makes the code more concise (a > b) ? (a > c ? cout << a << " is the largest number of given three numbers" : cout << c << " is the largest number of given three numbers") : (b > c ? cout << b << " is the largest number of given three numbers" : cout << c << " is the largest number of given three numbers"); return 0; } |

**Output:**

1 2 3 4 5 |
Enter any three numbers 7 8 9 9 is the largest number of given three numbers |

I have already mentioned this example above, the purpose of mentioning it again is to make it clear that you can also print the results by directly writing the *cout* statements in place of 2^{nd} and 3^{rd} operands.

Hope, you find this article helpful. Kindly let me know about you feedback in the comments below.

Have a good day and happy programming :)