C++ Ternary Operator
In this tutorial, we'll learn about the C++ ternary operator and how to utilise it to manage the program's flow using examples.
Ternary Operator:
The if-else statement and the conditional operator use the same technique, however the if-else statement takes up more space, and the conditional operator helps to create the if-else statements as quickly as possible.
Syntax:
The Ternary Operator has the following syntax:
Var = St1 ? St2 : St3
Or the syntax will also take the following form:
Var= (Con) ? St2 : St3
Or syntax will also take the following form:
(Con) ? (Var = St2) : (Var = St3)
[Where Con = Condition, St = Statement, Var = Variable]
It can be represented as an if-else expression like this:
if(St1)
{
Var = St2;
}
else
{
Var = St3;
}
How it words?
The condition that needs to be assessed in this case is Statement1. Statement2 will be carried out, and the outcome will be returned, if the Statement1 condition is True. Otherwise, Statement3 will be run and the outcome will be returned if the condition (Statement1) is false.
Example 1:
C++ program to determine whether a number is even or odd using the ternary operator
C++ Program:
#include <iostream>
using namespace std;
int main()
{
//considering a variable as num
int num;
//asks user for an input
cout << "Put an integer number: ";
//storing value in the variable
cin >> num;
//using ternary operator
(num % 2 == 0) ? cout << num << " is an even number." : cout << num << " is an odd number.";
return 0;
}
If the user puts 9 as input,
Output 1:
Put an integer number: 9
9 is an odd number.
If the user puts 6 as input,
Output 2:
Put an integer number: 6
6 is an even number.
Case 1: The user-inputted value 9 is allocated to the variable num in the program shown above. The ternary operator is then used to determine if the integer is even or odd.
The expression (number % 2 == 0) produces an odd value because 9 is an odd integer.
Case 2: The user-inputted value 6 is allocated to the variable num in the program shown above. The ternary operator is then used to determine if the integer is even or odd.
The expression (number % 2 == 0) produces an even value because 6 is an even integer.
Example 2:
Find the largest number between the two numbers using a C++ program.
C++ Program:
#include <iostream>
using namespace std;
int main()
{
//declaring variables
int num1 , num2 , maximum;
//asks user for an input and storing value in the variable num1
cout << "Put number one: " ;
cin >> num1;
//asks user for an input and storing value in the variable num2
cout << "Put number two: " ;
cin >> num2;
//using ternary operator
maximum = (num1 > num2) ? num1 : num2;
// Printing maximum number
cout << "The maximum number between " << num1
<< " and " << num2
<< " is " << maximum;
return 0;
}
If the user puts 15 and 26 as inputs,
Output 1:
Put number one: 15
Put number two: 26
The maximum number between 15 and 26 is 26
If the user puts 47 and 35 as inputs,
Output 2:
Put number one: 47
Put number two: 35
The maximum number between 47 and 35 is 47