+ 8
Hi,can anybody explain this c++ output please ?
Why the output of this code : int a; a= 5>4?3>8?40:20:30; cout<<a; is 20 ? Thanks for help !
5 Answers
+ 5
That is the syntax of nested ternary operator. There's more than just assignment operations that can be done with it. You can even print statements using the ternary operator.
eg.
int main(){
int i=1;
((i>0)?printf("Y"):printf("N"));
return 0;
}
+ 8
It is the ternary operator.
The syntax is
Condition ? If True : if false ;
Here
a = ( 5 > 4 ) ? => true
3 > 8 ? True 40 : False 20
+ 8
Thanks for your help !
+ 4
In case of ternary operator (: is joined to ? From right hand side)
5>4 true
3>8?40:20 this whole thing is evaluated
Now 3>8 is false i.e 20
+ 3
Thanks !