C++ | Nested Ternary Operator
Last Updated :
09 Dec, 2021
Ternary operator also known as conditional operator uses three operands to perform operation.
Syntax :
op1 ? op2 : op3;
Nested Ternary operator: Ternary operator can be nested. A nested ternary operator can have many forms like :
- a ? b : c
- a ? b: c ? d : e ? f : g ? h : i
- a ? b ? c : d : e
Let us understand the syntaxes one by one :
- a ? b : c => This ternary operator is similar to if-else statement. So it can be expressed in form of if-else statement.
Expression using Ternary operator:
a ? b : c
Expression using if else statement:
if ( a )
then b execute
else
c execute
2.Example:
C++
// C++ program to illustrate
// nested ternary operators
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "Execute expression using"
<< " ternary operator: ";
// Execute expression using
// ternary operator
int a = 2 > 5 ? 2 : 5;
cout << a << endl;
cout << "Execute expression using "
<< "if else statement: ";
// Execute expression using if else
if ( 2 > 5)
cout << "2";
else
cout << "5";
return 0;
}
Output: Execute expression using ternary operator: 5
Execute expression using if else statement: 5
2. a ? b: c ? d : e ? f : g ? h : i =>This Nested ternary operator can be broken into if, else and else-if statement. The expression can break into smaller piece in ternary operator and if else statement which are given below:
Expression using ternary operator:
a ? b
: c ? d
: e ? f
: g ? h
: i
Expression using if else statement:
if a then b
else if c then d
else if e then f
else if g then h
else i
CPP
// C++ program to illustrate
// nested ternary operators
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "Execute expression using "
<< "ternary operator: ";
int a = 2 > 3 ? 2 : 3 > 4 ? 3 : 4;
cout << a << endl;
cout << "Execute expression using "
<< "if else statement: ";
if ( 2 > 3 )
cout << "2";
else if ( 3 > 4 )
cout << "3";
else
cout << "4";
return 0;
}
Output: Execute expression using ternary operator: 4
Execute expression using if else statement: 4
3. a ? b ? c : d : e => Below is the expansion of expression using ternary operator and if else statement.
Expression using ternary operator:
a ?
b ? c
: d
: e
Expression using if else statement:
if ( a )
if ( b )
c execute
else
d execute
else
e execute
CPP
// C++ program to illustrate
// nested ternary operators
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "Execute expression using "
<< "ternary operator: ";
int a = 4 > 3 ? 2 > 4 ? 2 : 4 : 3;
cout << a << endl;
cout << "Execute expression using "
<< "if else statement: ";
if ( 4 > 3 )
if ( 2 > 4 )
cout << "2";
else
cout << "4";
else
cout << "3";
return 0;
}
Output: Execute expression using ternary operator: 4
Execute expression using if else statement: 4
//improved by sathiyamoorthics19
Example 2: Evaluate the following statement.
5 > 2 ? 4 > 1 ? 5>7 ? 10 : 5 > 8 ? 6 > 2 ? 20 : 30 : 5 > 6 ? 40 : 50 : 7 > 2 ? 60 : 70 : 8 > 9 ? 80 : 90 ;
To solve the above problem, Grouping concept must be known.
First, check there is an equal number of colon and question mark.If it is equal, the statement is valid, if not it is an error statement.
- Come to first colon ( : ) and match the left nearest question mark ( ? )
- Repeat the process and continue until no colon ( : ) left
Key points :
First question mark ( ? ) to colon ( : )is considered as expression 2 and from that colon (:) to last is considered as expression 3
( i ) 5 > 2 is true, so come to the expression 2 which is question mark ( ? ) to colon ( : )is considered as expression 2 and execute.
( ii ) 4 > 1 is true, so come to the expression 2 and execute.
( iii ) 5 > 7 is false, so come to expression 3 and execute.
( iv ) 5 > 8 is false ,so come to expression 3 and execute.
( v ) 5 > 6 is false again, so come to expression 3 and the answer is 50.
C
#include <stdio.h>
int main()
{
int result;
result =5>2 ? 4 > 1 ? 5>7 ? 10 : 5 > 8 ? 6 > 2 ? 20 : 30 : 5 > 6 ? 40 : 50 : 7 > 2 ? 60 : 70 : 8 > 9 ? 80 : 90 ;
printf("Output : %d", result);
return 0;
}
//improved by sathiyamoorthics19
Similar Reads
Unary Operators in C
In C programming, unary operators are operators that operate on a single operand. These operators are used to perform operations such as negation, incrementing or decrementing a variable, or checking the size of a variable. They provide a way to modify or manipulate the value of a single variable in
5 min read
Unary Operators In C++
In C++, unary operators are the type of operators that work on a single value (operand). They perform operations like changing a value's sign, incrementing or decrementing it by one, or obtaining its address.C++ has a total of 9 unary operators:Table of ContentIncrement Operator (++)Decrement Operat
6 min read
Java Ternary Operator
Operators constitute the basic building block of any programming language. Java provides many types of operators that can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provi
5 min read
C++ Ternary or Conditional Operator
In C++, the ternary or conditional operator ( ? : ) is the shortest form of writing conditional statements. It can be used as an inline conditional statement in place of if-else to execute some conditional code.Example:C++#include <iostream> using namespace std; int main() { int x = 10, y = 20
3 min read
Conditional or Ternary Operator (?:) in C
The conditional operator in C is kind of similar to the if-else statement as it follows 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. It is also known as the ternary operator in C as it
3 min read
Ternary Search in C++
Ternary search is an efficient search algorithm used to find an element in a sorted array. It is a more efficient version of the binary search. In this article, we will learn how to implement the ternary search in C++.How Ternary Search Works?Ternary search divides the array (or search space) into t
4 min read
Ternary Search in C
When searching for a specific element in a sorted dataset, many programmers are familiar with binary search. Binary search efficiently narrows down the search space by dividing it into two halves repeatedly. But there's another search algorithm that can be even more efficient in certain scenarios. T
4 min read
Relational Operators in C
In C, relational operators are the symbols that are used for comparison between two values to understand the type of relationship a pair of numbers shares. The result that we get after the relational operation is a boolean value, that tells whether the comparison is true or false. Relational operato
4 min read
C/C++ Ternary Operator - Some Interesting Observations
Predict the output of following C++ program. c #include <iostream> using namespace std; int main() { int test = 0; cout << "First  character " << '1' << endl; cout << "Second character " << (test ? 3 : '1') << endl; return 0; } One would
3 min read
Java Ternary Operator Puzzle
In Java, the ternary operator is a simple way to perform conditional operations. It evaluates a condition and returns values depending on whether the condition is true or false. In this article, we will solve a Java program that uses the ternary operator with mixed operand types. Understanding the T
3 min read