Ternary Operator for Quick Decisions
C++
Medium
3 views
Problem Description
Use ternary operator (? :) as shorthand if-else.
Real Life: Quick one-line decisions in code.
Step-by-Step Logic:
1. Syntax: condition ? value_if_true : value_if_false
2. Checks condition
3. Returns first value if true, second if false
4. Makes code shorter and cleaner
Official Solution
void operator_q8_ternary() {
int age = 20;
string result = (age >= 18) ? "Adult" : "Minor";
cout << "Age " << age << " is: " << result << endl;
int a = 15, b = 25;
int max = (a > b) ? a : b;
cout << "Maximum of " << a << " and " << b << " is: " << max << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!