How to Use a Conditional Operator in C++

Jinku Hu Feb 02, 2024
  1. Use a Conditional Operator as an rvalue Expression in C++
  2. Use a Conditional Operator as an lvalue Expression in C++
How to Use a Conditional Operator in C++

This article will explain how to utilize a C++ conditional operator.

Use a Conditional Operator as an rvalue Expression in C++

Apart from the common arithmetic, logical, and assignment operators, C++ provides some special operators, one of which is a conditional ternary operator. Ternary implies that the operator takes three operands. It’s called a conditional operator because it works similarly to the if-else statement. The operator has the form of E1 ? E2 : E3, where the first operand can be treated as the if condition, which is evaluated and converted into the bool value.

If the bool value is true, the following expression (E2) is evaluated, and its side effects take place. Otherwise, the third expression (E3) is evaluated with its side effects. Notice that we can utilize this operator as the rvalue expression to assign a value to a variable conditionally. In the following example code, we read an integer from user input and evaluate the comparison expression input > 10, representing the E1 operand in the conditional operator. The variable n is assigned the input value only when E1 is true.

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
  int input;

  cout << "Enter a single integer: ";
  cin >> input;
  int n = input > 10 ? input : 10;

  cout << "n = " << n << endl;

  return EXIT_SUCCESS;
}

Output:

Enter a single integer: 21
n = 21

Use a Conditional Operator as an lvalue Expression in C++

Alternatively, we can utilize the ternary operator as an lvalue expression to conditionally choose a variable name on which the assignment operation is conducted. Note that we only specify the variable names as the second and the third operands. Generally, though, you can specify the expressions such as cout, which has external side effects; these effects get executed as usual.

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
  int input;

  cout << "Enter a single integer: ";
  cin >> input;
  int n = input > 10 ? input : 10;

  cout << "n = " << n << endl;

  int m = 30;
  (n == m ? n : m) = (m * 10) + 2;

  cout << "m = " << m << endl;

  return EXIT_SUCCESS;
}

Output:

Enter a single integer: 21
n = 21
m = 302

Another use case for the ternary operator can be in the class definition. The following code sample demonstrates such a scenario, where we implemented a recursive constructor for a MyClass structure similar to a node in a singly-linked list. In this case, we specify the constructor call as the second operand, called with the next argument, and continued the recursive call stack until the node.next evaluates to a false value. The latter is only the case when the node.next pointer is nullptr.

#include <iostream>
#include <string>

struct MyClass {
  MyClass* next;
  int data;

  MyClass(const MyClass& node)
      : next(node.next ? new MyClass(*node.next) : nullptr), data(node.data) {}

  MyClass(int d) : next(nullptr), data(d) {}

  ~MyClass() { delete next; }
};

int main() { return EXIT_SUCCESS; }
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Operator