jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Conditional Operator doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Conditional Operator doubt?" Watch "Conditional Operator doubt?" New topic
Author

Conditional Operator doubt?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hi Guys,

Consider the following code and please help me to interpret the same.

boolean b1 = false?false:true?false:true?false:true;

How to group them to find the solution??


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The ternary operator is right-associative so



is the same as

.
[ December 06, 2006: Message edited by: Keith Lynn ]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

What will be the result of that?? How condiotional operator works in the above case??
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
In any ternary operator, the form is

Micheal John
Ranch Hand

Joined: Nov 01, 2006
Posts: 344
and you have to solve it from right to left


Micheal John
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Micheal John:
and you have to solve it from right to left



Why? Once it is correctly parenthesized it can be evaluated more efficiently from left to right.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Harish Paravasthu
Greenhorn

Joined: Dec 21, 2006
Posts: 23
Haii,

if its formatted in braces,then go according to it..else go for right to left as said above.

answer is false...

harish


Harish Paravasthu
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Conditional Operator doubt?
 
Similar Threads
Conditional if
if-else
evaluation of expression
group conditional operators
Conditional Operator ?: