• Create Account

## How to use bool variable.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

47 replies to this topic

### #1xiajia  Members

Posted 24 December 2012 - 12:19 AM

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, 24 December 2012 - 03:48 AM.

### #2Sik_the_hedgehog  Members

Posted 24 December 2012 - 12:27 AM

POPULAR

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.

Edited by Sik_the_hedgehog, 24 December 2012 - 12:28 AM.

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.

### #3LennyLen  GDNet+

Posted 24 December 2012 - 12:29 AM

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.

### #4iMalc  Members

Posted 24 December 2012 - 12:43 AM

POPULAR

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

### #5Khatharr  Members

Posted 24 December 2012 - 01:00 AM

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.

### #6LennyLen  GDNet+

Posted 24 December 2012 - 01:36 AM

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. ;)

### #7Khatharr  Members

Posted 24 December 2012 - 03:07 AM

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.

### #8xiajia  Members

Posted 24 December 2012 - 04:24 AM

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, 24 December 2012 - 09:24 PM.

### #9rip-off  Moderators

Posted 24 December 2012 - 08:33 AM

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.

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.

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

What are the advantages of doing?

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.

### #10MarkS  Members

Posted 24 December 2012 - 10:52 AM

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, 24 December 2012 - 11:25 AM.

### #11Servant of the Lord  Members

Posted 24 December 2012 - 11:55 AM

if() statements take a bool. Think of it like a function, taking a single bool parameter.

Observe:

int myInteger = 0;
if(myInteger == 0)

The '==' symbol is actually a function with special syntax:

bool operator==(int, int)

It takes two parameters (one on the left of the == and one on the right), and returns a bool that is 'true' if they are both equal.

Try this code:

bool theTwoAreEqual = (myInteger == 0);

if() statements, being similar to (but not exactly) a function that takes a single parameter, and that parameter is always a single bool.

bool equation = (myInteger == 0);
if(equation)
{
//...do stuff...
}

You don't need to say:

if((myInteger == 0) == true)

bools are already true or false. if() statements already take a bool. The code:

if(myBool == true)

...is unnecessarily redundant, but a perfectly acceptable coding style.

The == function takes two values and returns a bool if they are equal.

The != function takes two values and returns a bool if they are not equal.

Likewise the < > <= >= functions return bools.

The ! operator also returns a bool.

The && and || operators also return bools:

bool result = (boolOne && boolTwo) || !boolThree;

If you have a bool already, you might as well just pass it to the if() statement, but there's no real harm in doing it the other way.

If statements say, "if(whatever in here is true)". Bools already know whether they are true or not without having to do comparison.

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #12Yrjö P.  Members

Posted 24 December 2012 - 12:26 PM

If there's one thing a beginner should be mindful of when writing conditional statements, it is to write comparisons with const value first. "if(true == a)" instead of "if(a == true)" because it prevents accidental assignment.

But when a is bool, "if(a)" is best anyway.

Edited by Stroppy Katamari, 24 December 2012 - 12:27 PM.

### #13Álvaro  Members

Posted 24 December 2012 - 12:51 PM

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.

You can make things more readable by making it clear in the name of a method that it returns a boolean. You can then say:
  if (recording.is_paused()) {
...

### #14MarkS  Members

Posted 24 December 2012 - 01:04 PM

If there's one thing a beginner should be mindful of when writing conditional statements, it is to write comparisons with const value first. "if(true == a)" instead of "if(a == true)" because it prevents accidental assignment.

But when a is bool, "if(a)" is best anyway.

Bull. That notation needs to die a horrible death! It is unnecessary and looks stupid. They mean the same thing, but one reads really weird. You truly gain nothing from that.

Edited by MarkS, 24 December 2012 - 01:06 PM.

### #15MarkS  Members

Posted 24 December 2012 - 01:05 PM

Edited by MarkS, 24 December 2012 - 01:06 PM.

### #16Cornstalks  Members

Posted 24 December 2012 - 02:05 PM

If there's one thing a beginner should be mindful of when writing conditional statements, it is to write comparisons with const value first. "if(true == a)" instead of "if(a == true)" because it prevents accidental assignment.

But when a is bool, "if(a)" is best anyway.

Bull. That notation needs to die a horrible death! It is unnecessary and looks stupid. They mean the same thing, but one reads really weird. You truly gain nothing from that.

I've seen bugs introduced because a programmer accidentally wrote if (someVar = SOME_VALUE) instead of if (someVar == SOME_VALUE). It's not very frequent, but this bug does exist in production code in various products. And beginners are more likely to make it themselves (I remember doing it myself). At the last place I worked at the coding standard was to do if (SOME_VALUE == someVar) simply to guard against the rare chance that there's a typo when writing the if statement.

I've found that, at least for me, I've gotten to the point where various rules and coding standards don't bother me anymore. I have my own personal style, but after having worked enough with other coding styles, it's no longer knives in my eyes when curly braces aren't on their own line, or the variable is on the right side when doing a comparison, or when members are prefixed with m_ (or some variant), etc.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #17Álvaro  Members

Posted 24 December 2012 - 02:09 PM

I've seen bugs introduced because a programmer accidentally wrote if (someVar = SOME_VALUE) instead of if (someVar == SOME_VALUE). It's not very frequent, but this bug does exist in production code in various products. And beginners are more likely to make it themselves (I remember doing it myself). At the last place I worked at the coding standard was to do if (SOME_VALUE == someVar) simply to guard against the rare chance that there's a typo when writing the if statement.

I think if you are disciplined enough to remember to put the constant first, you are also disciplined enough to use the correct operator. A much better solution is to set up your compiler so it will warn you if you write an assignment where you probably meant to write a condition.

bool foo() {
bool a = false;
if (a = true)
return true;
return false;
}

> g++ test.cpp -c -Wall
test.cpp: In function ‘bool foo()’:
test.cpp:3:15: warning: suggest parentheses around assignment used as truth value

### #18superman3275  Members

Posted 24 December 2012 - 02:20 PM

I've seen bugs introduced because a programmer accidentally wrote if (someVar = SOME_VALUE) instead of if (someVar == SOME_VALUE). It's not very frequent, but this bug does exist in production code in various products. And beginners are more likely to make it themselves (I remember doing it myself). At the last place I worked at the coding standard was to do if (SOME_VALUE == someVar) simply to guard against the rare chance that there's a typo when writing the if statement.

I think if you are disciplined enough to remember to put the constant first, you are also disciplined enough to use the correct operator. A much better solution is to set up your compiler so it will warn you if you write an assignment where you probably meant to write a condition.

bool foo() {
bool a = false;
if (a = true)
return true;
return false;
}

> g++ test.cpp -c -Wall
test.cpp: In function ‘bool foo()’:
test.cpp:3:15: warning: suggest parentheses around assignment used as truth value

Although I agree with CornStalks about style, Alvaro made a good point.

Specifically remembering to compare using

if(VALUE == VARIABLE)
{
//code
//code
}

is pointless, considering if you remember to specifically compare in this order you will in-turn remember to use ==.

I would say you should use if (a) mainly because if(a == true) is simply extra typing, and it will help later on if(object.member_variable) is better than if(object.member_variable == true) because when you have numbers that you must compare to true or false the Compiler will give you a warning when you say if(number == boolean). (Imagine loading a binary file and comparing the values given).

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

or Personal-Message me on here !

### #19Cornstalks  Members

Posted 24 December 2012 - 02:53 PM

I think if you are disciplined enough to remember to put the constant first, you are also disciplined enough to use the correct operator.
The problem isn't remembering, the problem is an accidental typo that gets overlooked. Sometimes you try to double-tap '=' but only one registers (maybe you accidentally pressed too softly the second time, or something). It's happened to me before (albeit very rarely).

A much better solution is to set up your compiler so it will warn you if you write an assignment where you probably meant to write a condition.
I agree, though I believe this style started before compilers warned of such things (I could be wrong on this, but it seems probable), and it's just stuck (perhaps to keep a consistent coding style). Yeah, we live in a modern age with decent compilers, but relying on that warning doesn't guard against everything:

// Compiling with LLVM from http://llvm.org/demo/index.cgi
int a = 1;
bool b = a = 42; // It doesn't catch this

if (a = 0) // But it catches this
{
b = false;
}


FWIW, I'm not advocating this use; I'm merely playing devil's advocate and saying it's not entirely useless. If you're writing a code base where a single bug can be catastrophic (like a missile/rocket guidance system, or robotic surgical system, or something), these "extra precautions" just might be worth it.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #20LennyLen  GDNet+

Posted 24 December 2012 - 04:19 PM

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

I wasn't actually trying to translate it literally into English. I was simply pointing out that in English you need a subject to compare a state to.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.