using

What Does ? Do in C++? The Ternary Operator

The ? in C++ is the ternary operator. It picks one of two values based on a condition, replacing a simple if-else in one line of code, with examples.

On this page
  1. The short answer
  2. A first example
  3. Why it is an expression
  4. Only one branch runs
  5. Choosing between two types
  6. Nesting
  7. Parentheses inside cout
  8. Ternary or if-else?
  9. Quick recap

The short answer

The ? in C++ is part of the conditional operator, which is also called the ternary operator. It takes three operands: a condition, a value to use if the condition is true, and a value to use if it is false. The ? and : symbols work together, and neither one does anything on its own.

condition ? value_if_true : value_if_false

The operator produces one of the two values. If the condition is true, the result is the first value. If the condition is false, the result is the second. Many references call it the conditional operator, so you may also find it under conditional operator in c.

A first example

#include <iostream>
#include <string>

int main() {
    int age = 20;
    std::string status = age >= 18 ? "adult" : "minor";
    std::cout << status << "\n";  // prints: adult
}

The condition age >= 18 uses a comparison operator, one of the comparison operators in c. Since 20 is at least 18, the condition is true and the result is "adult". The same job can be done with a regular if-else, which takes several lines, but the ternary keeps a simple choice on one line.

Why it is an expression

The ternary produces a value, so it can go anywhere a value is expected: when setting a variable, as a function argument, or in a return statement. An if statement is a statement, not a value, so it cannot sit in those places.

int larger(int a, int b) {
    return a > b ? a : b;
}

This function returns the bigger of two numbers.

Only one branch runs

Only the value that is chosen gets evaluated. The other one is skipped. This matters when the unused part would cause a problem:

#include <iostream>

int main() {
    int divisor = 0;
    int safe = divisor != 0 ? 100 / divisor : -1;
    std::cout << safe << "\n";  // prints: -1
}

The division 100 / divisor is never evaluated here, because the condition is false. The program never divides by zero.

Choosing between two types

The two values need types that the compiler can convert to a common type:

bool flag = true;
double x = flag ? 1 : 2.5;  // 1 is converted to 1.0

The next line does not compile, because a string literal and an int have no common type:

int n = flag ? "yes" : 42;

Nesting

You can put one ternary inside another. The operator groups from right to left:

int score = 85;
char grade = score >= 90 ? 'A' : score >= 80 ? 'B' : 'C';  // grade is 'B'

This reads as score >= 90 ? 'A' : (score >= 80 ? 'B' : 'C'). A short chain like this is fine, but a longer one is hard to scan. At that point an if-else chain is usually easier to read.

Parentheses inside cout

When you use a ternary inside cout, wrap it in parentheses:

#include <iostream>

int main() {
    int a = 3;
    int b = 7;
    std::cout << (a > b ? a : b) << "\n";  // prints: 7
}

The << operator binds more tightly than ?: and >. Without the parentheses, std::cout << a > b ? a : b is grouped as ((std::cout << a) > b) ? a : b, and that does not compile.

Ternary or if-else?

A ternary works well for a simple choice that produces one value. It is less suited to branches that run several statements. Each branch must be an expression, so a return statement or a block of code cannot go inside one. For those cases, use an if statement. The if else statement syntax in c shows the layout.

Quick recap

  • The ? and : symbols together form the conditional (ternary) operator.
  • The syntax is condition ? value_if_true : value_if_false.
  • Only the chosen value is evaluated.
  • Use parentheses when you put a ternary inside cout or another larger expression.
  • Keep nested ternaries short, or switch to an if-else chain.

The complete guide to using

The using Keyword in C++
Read the guide

Keep exploring

Guides from other topics

All topics →