can some1 please tell me what bools are, why they are used, and how do u use them...
can some1 please tell me what bools are, why they are used, and how do u use them...
Its just a boolean value. It can have a value of "true" or "false." They are nice since they make code a little more understandable to everyone.
Example:
Code:bool done = false; while(!done) { if(doSomething() == true) done = true; }
The if-statement tutorial has some good information about the use of true & false. (But I didn't see any bool type variables used there.)
bools are used to store two-state variables...
bool x = true; // OK
bool x = 5; // ERROR!
bool SwitchOn = true; // true if on, false if off
bool Male = true; // true if male, false if female
bool Cat = false; // true if cat, false if not a cat
if(Cat && !Male) // If it's a female cat...
Boolean logic is binary (2-state) logic. It uses AND, OR, EXCLUSIVE OR, and NOT (invert).
false is stored in memory as a zero, and true is stored in memory as a one.
When you use integers with boolean logic, zero is always false, and non-zero is always true. In an if-statement, (2 + 2) is true, and (2 - 2) is false.
Last edited by DougDbug; 10-04-2004 at 05:12 PM.
What they are has been explained enough. I personally use them to control while loops. I also use them on select functions, well currently for a collision detection function(return true on a hit false on not hitting).
Woop?
very helpful for a beginner like me. thanks you all.
i'm not trying to argue....but are you sure bool x = 5 is illegal? because the value 5 returns true...i know there's very few times you would even think about doing that, but it may be reasonable in some cases...
i have never tried though...
what about
bool x = (bool)5; ?
its not illegal, but in general a bool should be used as a bool, using an int to flag zero vs non-zero boolean logic is more intuitive IMO.Originally Posted by misplaced
ex (g++, Linux 2.6 Kernel, x86)
Code:bool b = 2345; cout << "BOOL: " << b << '\n' << "INT : " << (int)b << endl; output: BOOL: 1 INT : 1
Actually, bools are usually a byte in size. Thus bool x = 5 is legal. bool b = 2345 wouldn't be legal as it is too large. However, the extra bits will be truncated thus its not illegal to say 2345, it just would only output 41 as its value.
P.S. cout will treat a bool as a bool...thus it will only have a set and unset value. Try doing the same but typecast b as an int.
While legal, 'bool b = 42', etc. is an abuse of the abstraction and best avoided.
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
> While legal, 'bool b = 42', etc. is an abuse of the abstraction and best avoided.
Agreed.
i'm not saying i want to do that....but i think in some program somewhere you might want to test a certain values state without directly using the variable...for instance, repeated referencing of arrays. say you had an array of int's and you were going to reference element[x] a repeated number of times within a loop. for optimization you would do something like int current = element[x]. well, if you only want to know the elements state, you might as well do bool currentElementsState = element[x]
^ random code.
As far as giving a bool a value other than true or false, I would also point out that you would need to test a bool for a value rather than comparing it to true.
Example:
instead ofCode:bool x = someFunction(); // for the sake of argument, lets say some function returned 5 if(x) { // do stuff }
Which how I handle bools anyway, but others may prefer a more direct comparison. So again, one should just play by the rules even though they don't have to.Code:bool x = someFunction(); // for the sake of argument, lets say some function returned 5 if(x == true) { // do stuff }
this is inncorrect. If you assign a bool an integer value, you can compare it to "true" successfully. just try itOriginally Posted by master5001
Code:bool b = 2345; if( b == true ) cout << "b is true!!!" << endl;
I believe, although bool's are physically a whole byte (hard not to be), they only ever hold 1 or zero - and if you attempt to store another value in it, it will automatically be converted to either true or false according to 'if' logic. Perspective's test output backs this up:
Although, as Prelude once said, "my compiler does this" is not the same as "the standard says this".output:
BOOL: 1
INT : 1
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Theoreticly, "true" could be defined as "!0". Thus, if it was simply treated as an integer, the truth test could equal "true". I don't know if the C++ standard defines "true" as "must be one" or not. However in a "boolean test", anything which isn't false is considered true.
Quzah.
Hope is the first step on the road to disappointment.