• 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

How the conditional operator will work?

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am bit confused with conditional opearator. Please can any one, explain how it will be worked, with the complicated problem ( I know the simple conditional operation).
And at the same time, please explain how the below one will be evaluated,
String s1 = (b=!b)?(b=!b)?"Hello":"hello" b=!b)?"world":"World";
Thanks in Advance,
Narasimha.
[ February 06, 2004: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, let's start at the beginning. Is b supposed to be a boolean? If it is, we can go forward, if not, this won't even compile. For now, I'll assume that b is a boolean. Let's start with this:

We can determine how this will evaluate by adding some parenthesis to show the order of operations:

So, what does this mean? Well, first we evaluate b = !b. Well, that's really nothing more than an assignment statement - it isn't a logical comparison. We're assigning the complement of b (b is true, so its complement is false) to b. Therefore, b = !b evaluates to false.
With that, we go on to the 3 operand of the expression, which is the expression (b=!b)?"world":"World".
Again, we evaluate b = !b. At this point, b is false (from the first assignment) so we assign the complement of b, which is true, to b. That leads us to select the first resulting operand, which is "world."
So, if b is originally true, s1 will be "world".
Similarly, if b is originally false, s1 will be "hello".
I hope that helps,
Corey
[ February 06, 2004: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey,
this is a sort of question i was going to ask
I like the way you used the double bracket to help narasimha see what is happening, I have seen a question like this before, and i did not know how to view it.
I shall try to remember this for my own studies.
Cheers from me
Davy
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic