🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

does this mean "==true"

Started by
8 comments, last by Neeneko 17 years, 2 months ago
Hi, im doing alot of the nehe tutorials on OpenGL. Most of the if paramaters just contain a single variable with out any operators, for example 'if (fullscreen)'. Does that simply mean in other words 'if(fullscreen==true)' but if no variable value is defined to compare with the actual value, it will compare the actual value with true! thanks!
Advertisement
Simple said, if(fullscreen) and if(fullscreen == true) are exactly the same thing.
Also:

if(!fullscreen){    ...}


is the same as:

if(fullscreen == false){    ...}


Oh great, yeh sorri my wording was totaly wrong, thanks guys!
Well almost, it does have the same practical effect in most circumstances, but in reality it's realy anything larger than 0 is true, the rest is false.
So if fullscreen where 2 instead of 1, then if(fullscreen==true) and if(fullscreen) would not produce the same result.

So the correct statement would probably be if(fullscreen) is exactly the same as if(fullscreen>false).
Quote: Original post by lc_overlord
Well almost, it does have the same practical effect in most circumstances, but in reality it's realy anything larger than 0 is true, the rest is false.
So if fullscreen where 2 instead of 1, then if(fullscreen==true) and if(fullscreen) would not produce the same result.

So the correct statement would probably be if(fullscreen) is exactly the same as if(fullscreen>false).


No, not fullscreen > false at all. Any value which isn't exactly 0 is considered true.

But its best not to use math operators on bools anyway.
On a side note, you should never compare anything with true or false. Compare these two:
if(the sky is blue) ...
if(the sky is blue is true) ...

The first one is obviously less noisy. And compare:
if(the sky is not red) ...
if(the sky is red is false) ...

Again, the first one is is less noisy. This applies to all logical queries.
Oh yeh rite, great thanks guys, and Ahnfelt thanks for that last comment, Ive come from a programing background were I always use comparisions in the paramaters for if statments, but i will defintly remember not to for true false ones! Thanks!
Quote: No, not fullscreen > false at all. Any value which isn't exactly 0 is considered true.

But its best not to use math operators on bools anyway.


... in C++. In C, since there is no native bool type, if(intvalue) and if(intvalue == true) are not equivalent (the first evaluates an integer value in a boolean context, which equates veracity with non-zero, while the second may compare integers together depending on the definition of your true constant).

Because, after all, if(filenotfound) is different from if(filenotfound == true).
Quote: Original post by ToohrVyk
... in C++. In C, since there is no native bool type, if(intvalue) and if(intvalue == true) are not equivalent (the first evaluates an integer value in a boolean context, which equates veracity with non-zero, while the second may compare integers together depending on the definition of your true constant).



This can hold true in C++ sometimes too. With high optimization levels switched on some compliers get a bit fast and loose with making sure bools stay bools. For instance code that looks like:

bool foo() { return 3;}

Depending on what -O you are running at, this might return '1' or '3', which then interacts oddly with boolean tests later on.

This topic is closed to new replies.

Advertisement