How to use bool variable.

Started by
46 comments, last by Trienco 11 years, 3 months ago

When I use a variable of type bool.People warned me:you'd better written if(a == true) as if(a). I do not know why we must do this. What are the advantages of doing?

My opinion, simply follow the following principles: 'bool' defined variables must be used and can only use 'true' and 'false' initialization and assignment, in the judgment must be written as if (a == true) or if (a== false).
then can avoid all ambiguity and hidden meaning.

Is that true?

Advertisement

The reasoning behind that logic is this: if a is true, then a == true returns true. If a is false then a == true returns false. So... basically it's just redundant. Doesn't hurt if you explicitly make the comparison, though, so honestly don't bother much about it.

EDIT: also if somebody ever tells you that the comparison makes the program slower, tell them the compiler is smart enough to optimize it. I hope you don't meet such a person, but that could happen.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.
Your opinion is quite correct. Both if(a) and if(a == true) are equivalent, and while to an experience C/C++ coder there will be no ambiguity, having if(a == true) is clearer to people who are not so used to reading code.
If it seems clearer putting == true on the end, then you possibly aren't using good variable names.
People don't go around saying "if paused equals true then ...". They'd say "if paused then ..."

Besides, where do you stop the redundancy?:
if ((a == true) == true)
if (((a == true) == true) == true)

This is one of those times where I believe it's a case of "correct your thinking".
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

They compile exactly the same.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
People don't go around saying "if paused equals true then ...". They'd say "if paused then ..."
If you're going to compare to a natural language, you need to use the language correctly. Saying "If paused then..." is not correct English, as it lacks a subject. Used correctly you would say "If the recording is paused..." So the more verbose comparison (if(a == true)) is actually the form used in English.
This is one of those times where I believe it's a case of "correct your thinking".

Which could also be said in regard to your use of the English language. ;)

To clarify, the if statement simply checks for equality with zero. If the value provided is equal to zero then the test is considered 'false'. Otherwise it's considered 'true'. The bool values are typically defined "false = 0" and "true = !false". So if(x) can be thought of as "If x is non-zero then ...".

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

bool a;  //no initialized
if(a == true)
{
    //do 1
}
if(a == false)
{
   //do 2
}


Result is not 'do 1' nor 'do 2'.'a' is not initialized. 'a' is not 'true' nor 'false'.This is contrary to the definition of 'bool' logic.'bool' variable is either 'true' or 'false'.Can not have the other values.According to the interpretation of a friend before.'true' should be a logical value, rather than an actual value.That is 'true' should be '! false', rather than the other what the exact value.But that there is no way to express in a programming language.We can refer to a lot about bool definition.

in VS "windef.h"


typedef int BOOL;
#define FALSE 0
#define TRUE 1

in SDL


typedef enum 
{ 
    SDL_FALSE = 0, 
    SDL_TRUE  = 1
}SDL_bool;

People trying to use an exact value represents the 'true', But this logic is wrong.So I think that should be realized through the state variables to solve this problem.the method I mentioned before.
For example:


bool a = 2;
if(a)
{ 
//do true
}
if(!a)
{ 
//do false
}

the result is 'do true'.It is correct from logical?

If you're going to compare to a natural language, you need to use the language correctly. Saying "If paused then..." is not correct English, as it lacks a subject. Used correctly you would say "If the recording is paused..." So the more verbose comparison (if(a == true)) is actually the form used in English.

[/quote]

I don't follow this. The translation of "if(paused == true)" to english would be something like "If it is true that the recording is paused".

When I use a variable of type bool.People warned me:you'd better written if(a == true) as if(a). I do not know why we must do this.

[/quote]

I'd advise the opposite, don't compare with the literal value for boolean variables.

What are the advantages of doing?

[/quote]

It is mainly stylistic, though as others have mentioned there is actually a subtle difference between the two, as a boolean variable can actually have values other than true or false.

Besides, where do you stop the redundancy?:


if ((a == true) == true)

if (((a == true) == true) == true)

This is one of those times where I believe it's a case of "correct your thinking".

Really well put!

This topic is closed to new replies.

Advertisement