Updated April 17, 2023
Introduction to C++ ternary operator
In C++, the conditional operator, also known as the ternary operator, is similar to the conditional statement if-else as it has the same algorithm. Even though it is similar to the if-else statement, the ternary operator uses only less space and easy to implement. The ternary operator is denoted by “?”. The ?: operator checks an expression, and depending on that; it returns the one value from the two given values. In this article, we will see more on C++ ternary operator.
Syntax
Whatever we study in a programming language, the syntax is the first thing we have to look into. So, first, let us see the syntax of the ternary operator.
The ternary operator is of the form as shown below.
var = expr1 ? expr2 : expr3
Here, expr1 is the condition that has to be checked. While checking the expr1, if it is true, then expr2 will be executed. At the same time, if the condition is false, expr3 will be executed.
As already mentioned, this ternary operator is almost similar to the if-else statement. So, we will see how the same ternary operator will be in if-else visualization.
If(expr1)
{
var = expr2;
}
else
{
var = expr3;
}
How does ternary operator work in C++?
As we studied the syntax, let us see how the ternary operator works with some examples. Before moving to that, just think why this operator is termed as ternary.
Yes. As the number of operands that has to be worked in three, it is called ternary operators.
As I already said, we will see some samples for the same.
Example:
#define MAX( x , y ) ( ( ( x ) > ( y ) ) ? ( x ) : ( y ) )
In this sample, first the expression, max(x,y) is checked. That is, it checks where x > y. If x > y is true, then x is returned. At the same time, if the expression returns false, y is returned.
Similarly, we can use the ternary operator for selecting which value to be assigned to a given variable.
int big = (happy > happymoments) ? happy: happymoments ;
In the given an example, happymoments or happy will be assigned to the variable big based on the bigger value.
We can do a reverse process for the same. That is, a variable can be assigned a value.
(happy > happymoments) ? happy: happymoments) = big;
That is, big will be assigned to happy or happymoments based on the bigger value.
Note:
Mistakes are common. But, in programming languages, sometimes it takes our valuable time. In this ternary operator, the most common mistake programmers do is fail to recall the operator precedence. The operator ?: has low precedence, and as a result, the chances of making a mistake is high.
Examples
In the above section, we discussed some samples on the ternary operator. Now, let us see the working code with sample output for the same.
Example #1
CPP program to display the largest number of two numbers using the ternary operator.
Code:
// C++ program to find the biggest value with the help of ternary operator
#include <iostream>
using namespace std;
//main method
int main()
{
// declare the variables for storing the integers
int num1 = 35, num2 = 61;
//declare a variable for storing the bigger value.
int large;
// Find the large value using ternary operator
large = ( num1 > num2 ) ? num1 : num2 ;
// display the biggest number
cout << "Biggest number among both numbers " << num1 << " and " << num2 <<" is: " << large ;
return 0;
}
Output:
In this program, two numbers, 35 and 61, are declared first. Then, an additional integer variable is also declared to store the result of the ternary operator operation. Once this is done, the largest of 35 and 61 is given as a condition. Based on the ternary operator, the result will be displayed on executing the code. That is, result 61 will be displayed on the successful executing of code.
Let us see how the same logic will be implemented using the if-else condition.
Code:
#include <iostream>
using namespace std;
//main method
int main()
{
int num1 = 35;
if (num1 < 61)
{
cout << "Biggest number is 61" ;
}
else
{
cout << "Biggest number is " << num1 ;
}
return 0;
}
This program also displays an output where 61 is the bigger number among the two, as shown below.
So this proves that the ternary operator and if-else behaves similarly.
Example #2
CPP program to display the largest among two characters using the ternary operator.
Code:
// C++ program to find the biggest value with the help of ternary operator
#include <iostream>
using namespace std;
//main method
int main()
{
// declare the variables for storing the character values
char str1 = 'a';
char str2= 'b';
//declare a variable for storing the bigger value.
char large;
// Find the large value using ternary operator
large = (str1 > str2) ? str1 : str2 ;
// display the biggest number
cout << "Biggest number among both values " << str1 << " and " << str2 <<" is: " << large ;
return 0;
}
Output:
In this program also, all the necessary libraries are imported first. Then two-character values a and b are declared. In addition to that, an extra char variable large is also declared to store the bigger value. That is, a or b will be assigned to the variable big based on the bigger value. On executing the code, it can be seen that the value b gets printed as the larger value.
Conclusion
The conditional operator, also known as a ternary operator in C++, is similar to one of the conditional statement if-else. But, unlike the if-else, the ternary operator uses only less space and easy to implement. In this article, different aspects such as syntax, working, and examples of the ternary operator are explained in detail.
Recommended Articles
This is a guide to C++ ternary operator. Here we discuss How does ternary operators work in C++ and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –