Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

spillsome

true vs true

This topic is 6073 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

Somebody told me that if(myvariable==true) was not the same as if(true==myvariable) is that right ? if yes, what is the difference thanks

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
using if(true==myvariable)
you can prevent the tipical error "if(myvariable=true)" because true is a constant and you can''t assign values to constants...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
the difference is the order that they are evaluated in, when only looking at variables they are the same however if they are functions then there can be a difference:

  

bool myFunc(int a)
{
cout << a;
return true;
}

if(myFunc(5)==myFunc(3))

if(myFunc(3)==myFunc(5))



both if statements are true but will produce different results, the first will print 53 the second will print 35

Share this post


Link to post
Share on other sites
quote:
Original post by WildWest
I would prefer this:

if(myvariable)
{
}

Also, prevents you on the mistake mentioned above!

Wildwest


It also prevents someone who is reading your code from knowing if you are comparing a boolean to true, a pointer to NULL, or an integer to 0.
For clarity, I always write the entire explicit test.

Share this post


Link to post
Share on other sites

  • 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!