The conditional operator is a special operator used to choose one of two values or expressions based on a condition. It is also called the ternary operator because it uses three operands.
The conditional operator is used to make a simple decision in a single expression.
int age = 20;
int result = (age >= 18) ? 1 : 0;
Since the condition is true, result becomes 1.
The conditional operator is called a ternary operator because it works with three operands:
condition ? trueValue : falseValue;
The basic syntax is:
condition ? expression1 : expression2;
If the condition is true, expression1 is selected. If the condition is false, expression2 is selected.
int age = 20;
std::string result =
(age >= 18) ? "Adult" : "Minor";
The condition age >= 18 is true, so result becomes "Adult".
int a = 10;
int b = 20;
int larger = (a > b) ? a : b;
Since a > b is false, the value of b is selected. Therefore, larger becomes 20.
int a = 10;
int b = 20;
int smaller = (a < b) ? a : b;
The condition is true, so smaller becomes 10.
int number = 7;
std::string result =
(number % 2 == 0) ? "Even" : "Odd";
The remainder is not zero, so the result is "Odd".
int marks = 65;
std::string result =
(marks >= 40) ? "Pass" : "Fail";
Because marks are at least 40, the result is "Pass".
int number = -5;
std::string result =
(number >= 0) ? "Positive or Zero" : "Negative";
The condition is false, so the result is "Negative".
A Boolean condition can be used directly with the conditional operator.
bool isLoggedIn = true;
std::string message =
isLoggedIn ? "Welcome" : "Please login";
The result is "Welcome".
The selected result can be printed directly without storing it in another variable.
int age = 25;
std::cout <<
((age >= 18) ? "Adult" : "Minor");
#include <iostream>
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
std::cout <<
((number > 0) ? "Positive" : "Zero or Negative");
return 0;
}
char grade = 'A';
std::string result =
(grade == 'A') ? "Excellent" : "Other Grade";
If the grade is A, the first expression is selected.
#include <iostream>
#include <string>
int main() {
std::string name = "Rahul";
std::string message =
(name == "Rahul") ? "Name matched" : "Name did not match";
std::cout << message;
return 0;
}
A simple conditional assignment can often be written using either if-else or the conditional operator.
Using if-else:
int age = 20;
std::string result;
if (age >= 18) {
result = "Adult";
} else {
result = "Minor";
}
Using conditional operator:
std::string result =
(age >= 18) ? "Adult" : "Minor";
int a = 45;
int b = 30;
int greater = (a > b) ? a : b;
std::cout << "Greater = " << greater;
The result is 45.
int a = 45;
int b = 30;
int smaller = (a < b) ? a : b;
std::cout << "Smaller = " << smaller;
The result is 30.
int age = 22;
std::string result =
(age >= 18) ? "Eligible by age" : "Not eligible by age";
The condition checks whether the age is at least 18.
double temperature = 35.5;
std::string result =
(temperature > 30) ? "Hot" : "Normal";
Since the temperature is greater than 30, the result is "Hot".
int a = 20;
int b = 10;
int result = (a > b) ? a - b : b - a;
The condition is true, so a - b is selected and the result is 10.
A conditional operator can be nested inside another conditional operator. However, excessive nesting can make code difficult to read.
int marks = 75;
std::string result =
(marks >= 80) ? "A" :
(marks >= 60) ? "B" :
(marks >= 40) ? "C" : "F";
For complex decisions, an if-else if structure is often easier to understand.
The condition can contain logical operators.
int age = 25;
bool hasID = true;
std::string result =
(age >= 18 && hasID)
? "Allowed"
: "Not Allowed";
Both conditions must be true for the first result to be selected.
double price1 = 100.50;
double price2 = 150.75;
double lower =
(price1 < price2) ? price1 : price2;
The smaller price is stored in lower.
The conditional operator is frequently used on the right side of an assignment.
int number = 10;
int absoluteValue =
(number >= 0) ? number : -number;
If the number is negative, its negative value is selected to produce a non-negative result.
The two result expressions participate in determining the type of the conditional expression. Therefore, when the two expressions have different types, C++ may perform the appropriate conversions.
int age = 20;
double result =
(age >= 18) ? 100.0 : 50.0;
Both possible results here are double values.
The selected expression can also contain a function call.
#include <iostream>
void showAdult() {
std::cout << "Adult";
}
void showMinor() {
std::cout << "Minor";
}
int main() {
int age = 20;
(age >= 18) ? showAdult() : showMinor();
return 0;
}
Only the selected expression is evaluated.
#include <iostream>
int main() {
double purchase = 1500;
double discount =
(purchase >= 1000) ? 10.0 : 0.0;
std::cout << "Discount: "
<< discount << "%";
return 0;
}
The program assigns a 10% discount when the purchase amount is at least 1000.
#include <iostream>
#include <string>
int main() {
int marks;
std::cout << "Enter marks: ";
std::cin >> marks;
std::string result =
(marks >= 40) ? "Pass" : "Fail";
std::cout << "Result: " << result
<< std::endl;
std::string status =
(marks >= 75) ? "Good Score" : "Regular Score";
std::cout << "Status: " << status;
return 0;
}
| Part | Meaning | Example |
|---|---|---|
| Condition | Checks whether something is true or false | age >= 18 |
| ? | Starts the true expression | ? |
| True expression | Selected when condition is true | "Adult" |
| : | Separates true and false expressions | : |
| False expression | Selected when condition is false | "Minor" |
condition ? trueExpression : falseExpression;
Question: What is the output of the following C++ code?
int a = 10;
int b = 20;
int result = (a > b) ? a : b;