aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ternary Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ternary" Watch "ternary" New topic
Author

ternary

Joe Allen
Ranch Hand

Joined: Nov 14, 2012
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"

"There are no dumb questions" quoted from HFSJ. "To err is human; to forgive is divine"
OCPJP 6, OCEJWCD 6
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

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..


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Joe Allen
Ranch Hand

Joined: Nov 14, 2012
Posts: 93
Got it. Thank you very much.
vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
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

Joined: Aug 11, 2011
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

Joined: Jul 12, 2010
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

Joined: Nov 14, 2012
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

Joined: Aug 11, 2011
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

Joined: Jul 09, 2001
Posts: 1164
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


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ternary
 
Similar Threads
Clarification question for transactions and EJB container
Recursion-Prime factors
MindQ's 37 mock
evaluation of expression
deployment environment can only have complied file?