# Can someone help me please?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-16-2012
Ryan500
Can someone help me please?
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
• 07-16-2012
laserlight
Quote:

Originally Posted by Ryan500
im really finding it hard to understand the bool. Can someone give me a example of how expierence programmers would use it please.

What is bool, and what are your examples of it being used?
• 07-16-2012
Ryan500
anything really so i can understand it. Just finding it really hard to understand
• 07-16-2012
laserlight
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.
• 07-16-2012
Ryan500
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;```
• 07-16-2012
laserlight
Quote:

Originally Posted by Ryan500
Up to now i only no how to do this :
Code:

```bool test = true; bool test = !false;```

What do these two statements do?
• 07-16-2012
Ryan500
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...
• 07-16-2012
laserlight
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.
• 07-16-2012
Ryan500
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;                 }```
• 07-16-2012
laserlight
Whether it is right or wrong depends on what you are trying to do.

That said, why do you do this?
Code:

`bool number = 10;`
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?
• 07-16-2012
Ryan500
no didnt intend to do that , its hard to understand
• 07-16-2012
whiteflags
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.
Code:

`bool found = !false;`
!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.
• 07-16-2012
laserlight
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.
• 07-16-2012
Ryan500
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
• 07-16-2012
skyliner
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
Code:

```int var = 0; if (var == 1)     doSomething; else if (var == 0)     doSomethingElse;```
is exactly the same as:

Code:

```bool var = false; if (var == true)     doSomething; else if (var == false)     doSomethingElse;```
or
Code:

``` string var = "iEatBananas"; if (var == "iEatStrawberries")     doSomething; else if (var == "iEatBananas")     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:

` if (var == "airplane"); if ( var == 789);  or the dreaded  if (var == true);`
I hope that helps.

I apologize for the blunder and inaccuracy of my oversimplified statements. I was just trying to explain the concepts in a simple, accessible manner.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last