Ternary Operator vs If-Else in C/C++



In C/C++, both ternary operator and if-else statements are conditional statements: A condition statement expresses the relationship between two ideas or events. Where one is dependent on the other. If the condition is true, the if statement will return; otherwise, the else statement will return.

Ternary Operator

We know that the ternary operator is the conditional operator. Using this operator, we can check some conditions and do some tasks according to those conditions. Without using the ternary operator, we can also use the if-else conditions to do the same.

Following is The Syntax of the Ternary Operator:

condition ? expression_if_true : expression_if_false

Expression: The ternary operator is an expression, which means it returns a value. It can be used within other expressions, assignments, or function calls.

It provides a simple and compact way to simple conditional logic in a single line. It can be less readable than if-else for complex conditions or nested logic.

Implement The Ternary Operator

In the following example, we demonstrate the working of the ternary operator:

#include<iostream>
using namespace std;
int main() {
   int a = 10, b = 20;
   const int x = (a < b) ? a : b;
   cout << x;
}

Following is the output:

10

If-Else Statement

The if-else statement in C/C++ is a fundamental control structure that allows a computer to execute certain code block for the given condition.

Following is the syntax of the if-else statement:

if (condition) {
   // Code to execute if condition is true
} else {
   // Code to execute if condition is false
}

The if-else statement does not return a value directly; first, check the condition and then return the specified value if either condition is true or false.

It is more readable than the ternary operator for complex and multi-line conditional logic. It is suitable for a wide range of the conditional logic, including the complex logic.

Implement The If-Else Statement

In the following example, we demonstrate the working of the if-else statement:

#include<iostream>
using namespace std;
int main() {
   int a = 10, b = 20;
   int x;
   if(a < b) {
      x = a;
   } else {
      x = b;
   }
   cout << x;
}

Following is the output:

10

Implementation of Ternary and If-Else Together

In the following example, let's see the working of the ternary operator and if-else statement together in a C++:

#include<iostream>
using namespace std;
int main() {
   int age = 19;
   
   cout << "Result of ternary operator:\n";    
   string result = (age < 18) ? "You are not 18+" : "You are 18+";    
   cout << result << endl;
   
   cout << "Result of if-else statement:\n";    
   string res = "";    
   if (age < 18) {
      res = "You are not 18+";
   } else {
      res = "You are 18+";
   }    
   cout << res << endl;

   return 0;
}

Following is the output:

Result of ternary operator:
You are 18+
Result of if-else statement:
You are 18+

The effect of the ternary operator and if-else statement is the same in most of the cases. Sometimes in some situations, we cannot use the if-else statement. We have to use the ternary operator in that situation. One of these situations is assigning some value to some constant variable. We cannot assign values to constant variables using if-else conditions. But using the ternary operator, we can assign a value to some constant variable.

Updated on: 2025-06-09T18:40:42+05:30

731 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements