Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ternary

 
Joe Allen
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Hibernate Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thank you very much.
 
vishal mishra
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not got it please explain it from following step

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


Why it is a single if-else block what about this-:

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
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic