# ternary

Joe Allen
Ranch Hand
Posts: 93
How to explain this statement:

System.out.println(true?false?"7":false?"6":false?true?"5": true?"4":"3":"2":"1");//prints "2"

Saif Asif
Ranch Hand
Posts: 440
• 1
well lets break it down one by one .
Solve for the outer true first
This
true ? false ? "7" : false ? "6" : false ? true ? "5" : true ? "4" : "3" : "2": "1"
should now resolve to this
false ? "7" : false ? "6" : false ? true ? "5" : true ? "4" : "3" : "2"

Solving for the false now, it becomes.
false ? "6" : false ? true ? "5" : true ? "4" : "3" : "2"
Again solving for the false gives.
false ? true ? "5" : true ? "4" : "3" : "2"

Now we just have a single if-else block ( outer ) with the if condition as false. So we can just jump to the else part .
"2"

I hope I explained fine..

Joe Allen
Ranch Hand
Posts: 93
Got it. Thank you very much.

vishal mishra
Ranch Hand
Posts: 103
Not got it please explain it from following step

false ? true ? "5" : true ? "4" : "3" : "2":"1"

true ? "4" : "3" : "2":"1"

I am not getting what is happening here so please explain me too...

Thanks

Saif Asif
Ranch Hand
Posts: 440
true ? "4" : "3" : "2":"1"
This is not a valid statement. Only one IF and 3 else blocks. ( note that I didnt say NESTED else-if blocks ). THe statement you wrote is similar to the following code snippet

which is invalid.

Now the other statement
false ? true ? "5" : true ? "4" : "3" : "2" (there is no 1 in the end , it was solved by the previous step)
is equivalent to following code snippet

which will always result in 2. I hope I was clear in explaining it . Do post back for any further queries.

vishal mishra
Ranch Hand
Posts: 103
Saif Asif wrote:
true ? "4" : "3" : "2":"1"
This is not a valid statement. Only one IF and 3 else blocks. ( note that I didnt say NESTED else-if blocks ). THe statement you wrote is similar to the following code snippet

which is invalid.

Now the other statement
false ? true ? "5" : true ? "4" : "3" : "2" (there is no 1 in the end , it was solved by the previous step)
is equivalent to following code snippet

which will always result in 2. I hope I was clear in explaining it . Do post back for any further queries.

Thank you, this is crystal clear now, thanks a lot

Joe Allen
Ranch Hand
Posts: 93
Saif Asif,
It is very nice and clear that you used that if-else to explain it. It seems like there is an "else System.out.println("3");" missing for the "if..." that prints out 4.
Thanks again.

Saif Asif
Ranch Hand
Posts: 440
Saif Asif,
It is very nice and clear that you used that if-else to explain it. It seems like there is an "else System.out.println("3");" missing for the "if..." that prints out 4.
Thanks again.

Oh yes you are absolutely right , I corrected it as below. Thanks for pointing it out

Dan Drillich
Ranch Hand
Posts: 1183
Vishal,

It's much simpler because we simply get the outer 2. We never get to execute the inner statement. If we did we would get 5 (true ? "5": true ? "4": "3").

Saif Asif wrote:

false ? true ? "5" : true ? "4" : "3" : "2"

Now we just have a single if-else block ( outer ) with the if condition as false. So we can just jump to the else part .
"2"

I hope I explained fine..

Regards,
Dan