The ternary operator helps you choose between two values quickly in one line. It makes simple decisions easy to write and read.
Ternary operator in C++
Introduction
Syntax
C++
condition ? value_if_true : value_if_false;
The condition is checked first.
If the condition is true, the expression returns value_if_true, otherwise it returns value_if_false.
Examples
max to the bigger of a and b.C++
int a = 10, b = 20; int max = (a > b) ? a : b;
num is even and sets isEven accordingly.C++
bool isEven = (num % 2 == 0) ? true : false;
score is 50 or more, otherwise "Fail".C++
std::cout << ((score >= 50) ? "Pass" : "Fail") << std::endl;
Sample Program
This program asks for your age and uses the ternary operator to decide if you are an Adult or a Minor, then prints the result.
C++
#include <iostream> #include <string> int main() { int age; std::cout << "Enter your age: "; std::cin >> age; std::string category = (age >= 18) ? "Adult" : "Minor"; std::cout << "You are an " << category << "." << std::endl; return 0; }
Important Notes
The ternary operator is best for simple decisions. For complex logic, use if-else statements for clarity.
Both value_if_true and value_if_false should be of compatible types.
Summary
The ternary operator is a short way to choose between two values based on a condition.
It uses the syntax: condition ? value_if_true : value_if_false.
Use it to make your code shorter and easier to read for simple choices.
Practice
1. What does the ternary operator
condition ? value_if_true : value_if_false do in C++?easy
Solution
Step 1: Understand the ternary operator syntax
The ternary operator uses a condition followed by a question mark, then two values separated by a colon.Step 2: Identify its purpose
It returns the first value if the condition is true, otherwise the second value.Final Answer:
It chooses one of two values based on the condition's truth. -> Option DQuick Check:
condition ? true_value : false_value = chooses value [OK]
Hint: Remember: condition ? true : false picks one value [OK]
Common Mistakes:
- Thinking it creates loops
- Confusing it with variable declaration
- Assuming it comments code
2. Which of the following is the correct syntax for the ternary operator in C++?
easy
Solution
Step 1: Recall the ternary operator syntax
The correct syntax is: condition ? value_if_true : value_if_false;Step 2: Check each option
condition ? value_if_true : value_if_false; matches the correct syntax exactly; others have misplaced symbols or punctuation.Final Answer:
condition ? value_if_true : value_if_false; -> Option AQuick Check:
Syntax = condition ? true : false; [OK]
Hint: Remember: question mark before colon in ternary [OK]
Common Mistakes:
- Swapping question mark and colon
- Using comma instead of colon
- Adding extra parentheses incorrectly
3. What is the output of this C++ code?
int x = 10, y = 20; int max = (x > y) ? x : y; std::cout << max;
medium
Solution
Step 1: Evaluate the condition (x > y)
Since x is 10 and y is 20, (10 > 20) is false.Step 2: Apply the ternary operator
Because the condition is false, the value after the colon (y) is chosen, which is 20.Final Answer:
20 -> Option CQuick Check:
(10 > 20) ? 10 : 20 = 20 [OK]
Hint: Check condition result true or false to pick value [OK]
Common Mistakes:
- Choosing the first value always
- Confusing > with <
- Expecting boolean output
4. Find the error in this C++ code using the ternary operator:
int a = 5, b = 8; int result = a > b ? a, b; std::cout << result;
medium
Solution
Step 1: Identify the ternary operator syntax error
The code uses a comma instead of a colon between the two values after the condition.Step 2: Understand the correct syntax
The ternary operator requires a colon to separate true and false values: condition ? true_value : false_value.Final Answer:
Incorrect use of comma instead of colon in ternary operator -> Option BQuick Check:
Use ':' not ',' in ternary [OK]
Hint: Use ':' to separate values in ternary, not ',' [OK]
Common Mistakes:
- Using comma instead of colon
- Forgetting to separate values properly
- Assuming ternary works like comma operator
5. Given this code, what will be the value of
Note: Division by zero causes runtime error.
result? int x = 0; int y = 5; int result = (x != 0) ? (y / x) : y * 2; std::cout << result;
Note: Division by zero causes runtime error.
hard
Solution
Step 1: Evaluate the condition (x != 0)
Since x is 0, (0 != 0) is false.Step 2: Apply the ternary operator safely
Because the condition is false, the expression after the colon is chosen: y * 2 = 5 * 2 = 10.Final Answer:
10 -> Option AQuick Check:
Condition false, so result = y*2 = 10 [OK]
Hint: Check condition first to avoid division by zero [OK]
Common Mistakes:
- Assuming division runs even if condition false
- Ignoring division by zero risk
- Choosing wrong expression side
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
