In this article I am going to talk about C++ Short Hand If Else (Ternary Operator) , also we can call it a ternary operator because it consists of three operands. and it can be used to replace multiple lines of code with a single line.
Exploring the C++ Ternary Operator
In the world of C++ programming, there’s a handy tool called the ternary operator, also known as the short-hand if-else operator. It’s a compact way to make decisions in your code.
Advantages of Ternary Operator
- Conciseness: Ternary operator condenses the if-else statement into a single line, enhancing code readability and reducing clutter.
- Efficiency: Ternary operator can be more efficient than if-else constructs, especially when used appropriately. It may lead to better optimization by the compiler.
- Functional Programming: It aligns well with functional programming paradigms, where expressions are preferred over statements.
Understanding the Ternary Operator
In C++, the ternary operator takes the form:
1 |
Expression1 ? Expression2 : Expression3; |
So above is the syntax for the ternary operator, we have three expressions, you can see the use of colon (:) and question mark (?) in this syntax. it means that we are checking if Expression1 is true, we are going to execute the statement after that question mark that is Expression2, and in the else case if Expression1 is false we are going to execute the code or statement after the colon that is Expression3. the (?) is called a ternary operator because it requires three operands and can be used to replace if-else statements, which have the following form.
This is a practical example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<iostream> using namespace std; int main() { int number = 10; if (number >= 10) { cout << "Number is greater than 10 " << endl; } else { cout << "number is less than 10" << endl; } return 0; } |
No you can replace that with the shorthand if else like this .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<iostream> using namespace std; int main() { int number = 10; number >= 10 ? cout << "Number is greater than 10" << endl : cout << "Number is greater than 10" << endl; return 0; } |
So in the code, we have used the ternary operator and we are checking our conditions.
After run this will be the result
1 2 |
Number is greater than 10 Press any key to continue . . . |
OK now let’s just create another example, this time we are going to check the condition according to the user input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include<iostream> using namespace std; int main() { int a; int b; int c = 20; int sum; cout << "Please enter first number : " << endl; cin >> a; cout << "Please enter second number : " << endl; cin >> b; sum = a + b; sum >= c ? cout << " Sum is greater than c value " << endl : cout << "Sum is less than c value" << endl; return 0; } |
In the above code first, we get two numbers from the user, after that, we sum the numbers and check that value with a predefined value of c that we have.
after the run, this will be the result.
1 2 3 4 5 6 |
Please enter first number : 5 Please enter second number : 7 Sum is less than c value Press any key to continue . . . |
Final Thoughts
The ternary operator in C++ offers a nice and efficient means of expressing conditional logic. By condensing if-else statements into a single line, it enhances code readability and often results in more efficient execution. However, like any language feature, it should be used carefully and with consideration for readability.
Read More on C++
- C++ Introduction & Program Structure
- C++ Variables And Data Types
- C++ Getting User Input
- C++ If Else Statement
Subscribe and Get Free Video Courses & Articles in your Email