Archived

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

true vs true

This topic is 5823 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
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
I would prefer this:

if(myvariable)
{
}

Also, prevents you on the mistake mentioned above!

Wildwest

Share this post


Link to post
Share on other sites
A lot of code checking programs (PC-lint) don''t like "if"s with no explicit comparison.

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