+ 1
Nested ternary operator
Can someone explain me how this works ? int a = (30>20)?(4>1)?20:10:5; outputs T+T = 20 F+F = 5 T+F = 10 F+T = 5
4 ответов
+ 2
🇮🇳Vivek🇮🇳 you're missing a closing curly brace '}' for the nested else statement, but otherwise yes that is equivalent.
+ 1
if(30 >20) {
if(4>1) {
return 20
}else {
return 10
}else {
return 5
}
}
This is what I think.
+ 1
ChaoticDawg thanks for checking.
+ 1
#include <iostream>
using namespace std;
int main()
{
// Here is Nested ternary operator example
int a = (10>20)?(0>1)?20:10:5;
cout<<a<<endl;
// Solution with if-else
if (10>20) cout << "20"<<endl;
else if (0>1)cout << "10"<<endl;
else cout << "5" << endl;
return 0;
}
/*
OUTPUTS
T+T = 20
F+F = 5
T+F = 10
F+T = 5
*/
// Adding the code here hope it may help others. Thanks for your answers.