Jump to content
  • Advertisement
Sign in to follow this  
elitentity

When is a&&b different from b&&a

This topic is 4818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know that they can be different, but I need an example. What i mean is this: a&&b is true, b&&a is false Is there a way to get it? EX code: { // ...some code here System.out.println(a&&b +"\t" + b&&a)} output should be something like: true false Help? BTW, this is in java, if that makes a difference(I don't think it does).

Share this post


Link to post
Share on other sites
Advertisement
The only time that that would happen if you're doing something horrible with side-effects in the evaluation of a and/or b. Why do you want something like that?

Share this post


Link to post
Share on other sites
It's a homework assignment that i can't figure out.
I've done a lot of testing and I don't know how to create this situation.
I think you need to assign a and b to some kind of long condition that comes with a side effect, but I still haven't got it.

Share this post


Link to post
Share on other sites
I know you can define operators in C++, and Im pretty sure the same thing applies to Java in this case.

lets say a and b are of the type MyType and there is an operation

a && b

which is defined by the pseudo function

operation&&(MyType m1, MyType m2)
{

return m1 < m2;

}

so when you get a && b it evaluates to a < b, which is true
where as b && a evaluates to a > b which is false

hope that helps

Share this post


Link to post
Share on other sites
You can't define operator overloads in Java. And since this is a homework question, then I think I've given you enough of a hint already.

Share this post


Link to post
Share on other sites
ok since your teacher might not tell you, the lesson is:

Avoid using side-effect producing functions in a boolean expression. A program where a&&b != b&&a is a bad program.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!