Open In App

C++ Ternary or Conditional Operator

Last Updated : 08 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, the ternary or conditional operator ( ? : ) is the shortest form of writing conditional statements. It can be used as an inline conditional statement in place of if-else to execute some conditional code.

Example:

C++
#include <iostream>
using namespace std;

int main() {
    int x = 10, y = 20;

    // Using ternary operator
    int max_val = (x > y) ? x : y;

    cout << "The maximum value is " << max_val;
    return 0;
}

Output
The maximum value is 20

Explanation: Here, two integers x and y are initialized to 10 and 20, respectively. The ternary operator (x > y) ? x : y checks whether x is greater than y. If true, it assigns x to max_val. Otherwise, it assigns y. Since y is greater, max_val becomes 20.

Syntax of Ternary Operator ( ? : )

The syntax of the ternary (or conditional) operator is:

expression ? statement_1 : statement_2;

As the name suggests, the ternary operator works on three operands where

  • expression: Condition to be evaluated.
  • statement_1: Statement that will be executed if the expression evaluates to true.
  • statement_2: Code to be executed if the expression evaluates to false.

The above statement of the ternary operator is equivalent to the if-else statement given below:

if ( condition ) {
statement1;
}
else {
statement2;
}

Examples of Ternary Operator

The below examples demonstrate the use of ternary operator in C programs.

Assign Value Based on Condition

C++
#include <iostream>
using namespace std;

int main() {

    // Creating a variable
    int n, t = 40;

    // Assigning the value of num based on the value of test
    // variable
    n = t < 10 ? 10 : t + 10;

    printf("%d", n);
    return 0;
}

Output
50

Explanation: In the above code, we have used the ternary operator to assign the value of the variable n depending upon the value of another variable named t. It is one of the most common applications of this operator

Note: The ternary operator have third most lowest precedence, so we need to use the expressions such that we can avoid errors due to improper operator precedence management.

Largest of the Three Numbers

Like if-else statements, the ternary operator can also be nested inside one another. We can use this to shorten the code for complex conditions.

C++
#include <iostream>
using namespace std;

int main() {
    int A = 39, B = 10, C = 23;

    // Evaluate largest of three using ternary operator
    int res = (A > B) ? ((A > C) ? A : C) :
  				((B > C) ? B : C);

    cout << res;
    return 0;
}

Output
39

As we can see it is possible to nest ternary operators in one another but the code gets complex to read and understand. So, it is generally avoided to use nested ternary operators.

Ternary Operator vs If-Else Statements

The ternary operator should only be used for short conditional code. For larger code, the other conditional statements should be preferred. The below table highlights some more differences:

FeatureTernary OperatorIf-Else Statement
ReadabilityConcise and compact for simple conditionsClearer for complex conditions
ComplexityDifficult to use for nested conditionsHandles multiple conditions better
Use CaseSimple, one-line conditional assignmentsDecision-making logic with multiple steps


Next Article
Article Tags :
Practice Tags :

Similar Reads