+ 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 !

28th May 2019, 3:17 PM
gabi rete
gabi rete - avatar
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; }
17th Aug 2019, 8:11 AM
Arnold D'Souza
Arnold D'Souza - avatar
+ 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
28th May 2019, 3:23 PM
AZTECCO
AZTECCO - avatar
+ 8
Thanks for your help !
28th May 2019, 4:03 PM
gabi rete
gabi rete - avatar
+ 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
22nd Oct 2019, 6:45 AM
Shivam Kumar
Shivam Kumar - avatar
+ 3
Thanks !
17th Aug 2019, 9:04 AM
gabi rete
gabi rete - avatar