Hi , im really finding it hard to understand the bool. Can someone give me a example of how expierence programmers would use it please. Looked at the tutorials and there all the same.
Thanks
Hi , im really finding it hard to understand the bool. Can someone give me a example of how expierence programmers would use it please. Looked at the tutorials and there all the same.
Thanks
What is bool, and what are your examples of it being used?Originally Posted by Ryan500
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
anything really so i can understand it. Just finding it really hard to understand
Sorry, but as I cannot read your mind, I cannot help you. Basically, I can tell you a whole lot of things, but if they are just repeating what you failed to understand, I would be wasting my time. Hence, I want to know what you already know, or at least think you know. If you really cannot tell me that, then either you did not make an effort to learn, or you're really not cut out for programming.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
everyone is cut out for programming just depends if there into it. Up to now i only no how to do this :
Code:bool test = true; bool test = !false;
What do these two statements do?Originally Posted by Ryan500
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
First line make it true second line makes it false but i still don't get how to use it. Like what do you use it for etc...
Ah. Okay, imagine that you were writing some code to check if a number exists in a collection of numbers. You could start by setting a bool variable named found to false, then loop through the collection until you found the number, upon which you set found to true and stop looping. Thus, after the loop has ended, you can determine if the number was found by checking the value of the found variable.
This is just one example; this margin is too narrow for me to list all of them.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
oh ok so could you tell me if this is right?
Code:bool number = 10; bool number2 = !false; if (number == number2) { cout <<"This is your number : " << number << endl; cout <<"This is a false number : " << number2 << endl; }
Whether it is right or wrong depends on what you are trying to do.
That said, why do you do this?
As you should know, there are two possible boolean values, namely true and false. 10 is not one of them. Now, 10 will be converted to true, but did you really intend that?Code:bool number = 10;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
no didnt intend to do that , its hard to understand
Um, given laserlight's task, I would expect the collection of numbers to be something like int collection[N];
You would look in there for your number, going one element at a time, I'd expect.
As for the boolean called found, even though there is no found in your program, I would not do this.
!false uses the NOT operator (!) which makes the result the opposite. So now found is actually true. It would be better to be as simple as possible. The only time I've used NOT explicitly was actually in complex loop conditions. Sometimes using NOT is the simplest expression, most times not.Code:bool found = !false;
Well, I suggest that you continue with your learning. As you go on, ideas on how this fits together will come naturally. I could of course give you examples with loops, arrays, etc, but if you don't know them yet, you're just going to get more confused.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
iv been learning c++ for a while but iv been on and of cause i find it hard to learn cause i have learning difficulties
Ryan500;
Think of it this way:
A boolean variable, or bool for short, is nothing more than a special integer variable that can only hold two values: 1 or 0.
It's not a matter of true or false, there is no testing involved in the assignment. The c++ language uses the keywords true or false, but you do not need to get lost behind the meaning of the words. It could be Up or down, left or right on or off, etc. It doesn't matter. What matters is that the variable either has a value(1) or is empty(0).
That is all, a simple variable that can only hold two values.
In reality you do not even need to use bools. You can achieve the same results using other types of variables, int, floats or whatever:
e.g
is exactly the same as:Code:int var = 0; if (var == 1) doSomething; else if (var == 0) doSomethingElse;
orCode:bool var = false; if (var == true) doSomething; else if (var == false) doSomethingElse;
In all those examples the code below the 'ifs' will only run if 'var' is equal to whatever you are comparing it to., be it:Code:string var = "iEatBananas"; if (var == "iEatStrawberries") doSomething; else if (var == "iEatBananas") doSomethingElse;
I hope that helps.Code:if (var == "airplane"); if ( var == 789); or the dreaded if (var == true);
I apologize for the blunder and inaccuracy of my oversimplified statements. I was just trying to explain the concepts in a simple, accessible manner.