Advertisement

true vs true

Started by February 02, 2002 07:38 AM
5 comments, last by spillsome 22 years, 7 months ago
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
well, they arent the same in appearance, but other than that they are the same.
Ron FrazierKronos Softwarewww.kronos-software.comMiko & Molly - Taking Puzzle Games to A Whole New Dimension
Advertisement
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...
I would prefer this:

if(myvariable)
{
}

Also, prevents you on the mistake mentioned above!

Wildwest
The Wild Wild West - Desperado!
A lot of code checking programs (PC-lint) don''t like "if"s with no explicit comparison.
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
Advertisement
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.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers

This topic is closed to new replies.

Advertisement