Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Nested ternary operator C++

/*
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

*/
// 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;
}
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #Nested #ternary #operator
ADD COMMENT
Topic
Name
9+4 =