File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conditional Operator ?:

 
Ritu Kapoor
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic