C++ Ternary Operator
The ternary operator (also known as the conditional operator) is a shorthand way of writing if...else
statements in C++. It allows you to evaluate a condition and choose one of two values based on the result, all in a single line of code. While the ternary operator can make your code more compact, it should be used with caution for readability.
In this blog post, we’ll cover:
The ternary operator has the following syntax:
condition ? expression1 : expression2;
If the condition evaluates to true, expression1 is returned. If the condition is false, expression2 is returned.
The ternary operator can be used as a direct replacement for simple if...else
statements.
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
// Using the ternary operator to find the maximum of two numbers
int max = (a > b) ? a : b;
cout << "The maximum number is: " << max << endl;
return 0;
}
Output:
The maximum number is: 20
Explanation:
(a > b)
is checked.a
is assigned to max
; otherwise, the value of b
is assigned to max
.You can also nest ternary operators inside each other, although this can reduce the readability of the code.
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20, c = 15;
// Using a nested ternary operator to find the largest of three numbers
int largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
cout << "The largest number is: " << largest << endl;
return 0;
}
Output:
The largest number is: 20
Explanation:
a
is greater than b
.a
is greater than c
.b
is greater than c
.if...else
StatementsThe ternary operator can be used as a replacement for simple if...else
statements, but it’s best suited for situations where the logic is clear and straightforward.
if...else
with Ternary Operator
#include <iostream>
using namespace std;
int main() {
int number = 7;
// Replacing the if...else statement with a ternary operator
string result = (number % 2 == 0) ? "Even" : "Odd";
cout << "The number is " << result << "." << endl;
return 0;
}
Output:
The number is Odd.
Explanation:
(number % 2 == 0)
checks if the number is even."Even"
to the result
variable; otherwise, it assigns "Odd"
.if...else
statement.While the ternary operator is great for evaluating one condition at a time, it can also handle more complex cases by combining multiple conditions into a single expression.
#include <iostream>
using namespace std;
int main() {
int age = 25;
// Using ternary operator to categorize age into age groups
string category = (age < 13) ? "Child" :
(age < 20) ? "Teenager" :
(age < 60) ? "Adult" : "Senior";
cout << "Age category: " << category << endl;
return 0;
}
Output:
Age category: Adult
Explanation:
Child
, Teenager
, Adult
, Senior
).age
.While the ternary operator can make your code more compact, it is best used in situations where:
if...else
statements in simple cases.Although the ternary operator is concise, it can reduce the readability of your code when used incorrectly or excessively. Here are some best practices to follow:
if...else
statements.The ternary operator is not ideal in the following situations:
if...else
for clarity.if...else
.