Skip to main content
Engineering LibreTexts

6.6: Ternary operator

  • Page ID
    29062
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Conditional or Ternary Operator (?:) in C/C++

    The conditional operator is kind of similar to the if-else statement as it does follow the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.

    The conditional operator is of the form

    variable = Expression1 ? Expression2 : Expression3

    In this statement Expression1 is the condition. If the condition is true, then variable is assigned the value from Expression2, else variable is assigned the value of Expression2. It would be the same as:

    if(Expression1)
    {
        variable = Expression2;
    }
    else
    {
        variable = Expression3;
    }
    

    Since the Conditional Operator ‘?:’ takes three operands to work, it is also called ternary operators.

    #include <iostream>
    using namespace std;
    
    int main()
    {
        // variable declaration
        int num1 = 5, num2 = 10, maxNum;
    
        // If num1 > num2 
        //    maxNum = num1
        // else 
        //    maxNum = num2
        maxNum = (num1 > num2) ? num1 : num2;
    
        // Print the largest number
        cout << "Largest number between "
            << num1 << " and "
            << num2 << " is "
            << maxNum << endl;
        return 0;
    } 

    In this case the output would be:

    Largest number between 5 and 10 is 10

    Now, if we wanted to get really crazy we could do something like the following. Decide the variable with the max value.

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int firstNum, secondNum, thirdNum, maxNum;
        firstNum = 55;
        secondNum = 33;
        thirdNum = 99;   
           
        //the following statement replaces the whole if-else
        //statement and makes the code more concise    
        maxNum = (firstNum > secondNum) ? (firstNum > thirdNum ? firstNum : thirdNum) : (secondNum > thirdNum ? secondNum : thirdNum);  
        cout << maxNum << "  is the largest number of given three numbers";
        
        return 0;
     }
    6 is the largest number of given three numbers

    Lets unpack this seemingly crazy C++ statement where nested ternary operators are used. (firstNum > secondNum) compares the value of firstNum and secondNum. If the value of firstNum is greater than value of secondNum, then (firstNum > thirdNum ? firstNum : thirdNum) will be executed that further compares the value of firstNum with the value of thirdNum. If the value of firstNum is greater than the value of thirdNum, the whole expression will become equivalent to the value of firstNumand assigned to maxNum. But if the value of firstNum is smaller than the value of thirdNum the whole expression will become equivalent to the value of thirdNum and assigned to maxNum.

    And if (firstNum > secondNum) becomes false, then (secondNum > thirdNum ? secondNum : thirdNum) will be executed that further compares the value of secondNum with the value of thirdNum. If the value of secondNum is greater than the value of thirdNum, the whole expression will become equivalent to the value of secondNum and assigned to maxNum. But if the value of secondNum is smaller than the value of thirdNum the whole expression will become equivalent to the value of thirdNum and assigned to maxNum.

    If it makes it easier to understand you could write this out with an if/else type of statement...but we won't do that here.

    Adapted from: 
    "Conditional or Ternary Operator (?:) in C/C++" by khalidbitdGeeks for Geeks is licensed under CC BY-SA 4.0 


    This page titled 6.6: Ternary operator is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.

    • Was this article helpful?