• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Conditional Operator ?:

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic