Ternary Operator ( ? : ) in C++

The conditional operator is also known as a ternary operator. It is used to write conditional operations provided by C++. The ‘?’ operator first checks the given condition, if the condition is true then the first expression is executed otherwise the second expression is executed. It is an alternative to an if-else condition in C++.

Syntax of Ternary Operator in C++

  condition ? expression1 : expression2

Flowchart of Conditional Operator in C++

Flow Diagram of Conditional operator

Example of Ternary Operator in C++

The below program demonstrates the use of a conditional operator to find the maximum of two numbers.

C++




// C++ program to demonstrate the use of ternary/conditional
// operator to find the max from two numbers
  
#include <iostream>
using namespace std;
  
int main()
{
    int num1 = 10, num2 = 40;
    int max;
    // if the condition is true then num1 will be printed
    // else num2 will printed
    max = (num1 > num2) ? num1 : num2;
    cout << max;
    return 0;
}


Output

40

Decision Making in C++

Decision-making in C++ involves the usage of conditional statements (also called decision control statements) to execute specific blocks of code primarily based on given situations and their results.

So basically, in decision-making, we evaluate the conditions and make a decision about which part of the code should be executed or not. It allows selective code execution which is crucial for controlling the flow of a program and making it more dynamic.

 

Similar Reads

Types of Decision-Making Statements in C++

In C++, the following decision-making statements are available:...

1. if in C++

In C++, the if statement is the simplest decision-making statement. It allows the execution of a block of code if the given condition is true. The body of the ‘if’ statement is executed only if the given condition is true....

2. if-else in C++

...

3. if-else if Ladder in C++

The if-else decision-making statement allows us to make a decision based on the evaluation of a given condition. If the given condition evaluates to true then the code inside the ‘if’ block is executed and in case the condition is false, the code inside the ‘else’ block is executed....

4. Nested if-else in C++

...

5. Switch Statement in C++

The if-else-if statements allow us to include additional situations after the preliminary if condition. The ‘else if’ condition is checked only if the above condition is not true. , and the `else` is the statement that will be executed if none of the above conditions is true. If some condition is true, then no only the associated block is executed....

6. Ternary Operator ( ? : ) in C++

...

7. Jump Statements in C++

The Nested if-else statement contains an ‘if’ statement inside another ‘if’ statement. This structure lets in more complex selection-making by way of comparing multiple conditions. In this type of statement, multiple conditions are checked, and then the body of the last if statement is executed....

Conclusion

...