• 11
• 14
• 19
• 27
• 9

How to use bool variable.

This topic is 1907 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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?

Edited by xiajia

Share on other sites
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.

Share on other sites

They compile exactly the same.

Share on other sites
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. ;)

Share on other sites

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 ...".

Share on other sites
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?

Edited by xiajia

Share on other sites

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.

Share on other sites

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!

Edited by MarkS