This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Conditional Operator ?: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Conditional Operator ?:" Watch "Conditional Operator ?:" New topic
Author

Conditional Operator ?:

Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
What about this. How this will be evaluated:

Code:
--------------------------------------------------------------------------
class EBH023 {
static String m1(boolean b){
return b?"T":"F";
}
public static void main(String [] args) {
boolean b1 = false?false:true?false:true?false:true;
boolean b2 = false?false true?false true?false:true));
boolean b3 = ((false?false:true)?false:true)?false:true;
System.out.println(m1(b1) + m1(b2) + m1(b3));
}
}
--------------------------------------------------------------------------
Output is: FFT
[ September 13, 2005: Message edited by: Barry Gaunt ]
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
Hi Ritu,
Consider it stepwise:
1)boolean b1 = false?false:true?false:true?false:true;
Evaluate from right to left in steps:
true?false:true = false
Then
true?false:false = false
Then
false?false:false
so finally , b1=false;

2)boolean b2 = false?false true?false true?false:true));
Here, the brackets are evaluated first starting with the innermost bracket on the right.
So you have:
(true?false:true) = false
Then
(true?false:false) = false
Then
(false?false:false) = false
so finally, b2 = false

3) boolean b3 = ((false?false:true)?false:true)?false:true;
Again the brackets are evaluated first, this time, the innermost bracket is on the left. So starting from LEFT,
(false?false:true) = true
Then
(true?false:true) = false
Then
false?false:true = true
So finally, b3 = true.

So we have now, m1(false) + m1(false) + m1(true)
and m1 returns T if true and F if false in (b?"T":"F")

So m1(false) = F
m1(false) = F
and m1(true) = T

Hence finally we get FFT.

Hope it is clear now ??
[ September 13, 2005: Message edited by: Barry Gaunt ]

<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
hi,
in the b1 can be modified without changing the output for simplification like this

here in b1
condition is false so it evaluates (true?false true?false:true)) inside it condition is true so it returns false
hence b1 is false.

boolean b2 = false?false: (true?false: (true?false:true));
here fist condition is false so it evaluates (true?false: (true?false:true)) now inside it condition is true so it again returns false
hence b2 is false

boolean b3 = ((false?false:true)?false:true)?false:true;
here first (false?false:true) is evaluated which results in true, so i becomes boolean b3 = (true?false:true)?false:true; now (true?false:true) is evaluated which results in false so it becomes becomes boolean b3 = false?false:true; and as now condition is false it returns true
hence b3 is true.

thus
System.out.println(m1(b1) + m1(b2) + m1(b3));
prints FFT

did it make you clear?
Sandy
[ September 13, 2005: Message edited by: Barry Gaunt ]

Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Note:To exterminate "smilies" please use the "disable smilies in this post" checkbox (scroll right the way down to see it) just before you submit your post.

Thanks,
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Hi,

I got the logic. That was so easy, and I didnot realize it.

Anyways Thanks to all of you!

Rgds,
Ritu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conditional Operator ?: