# Thread: New to C++ and confused by boolean and ifs and else

1. ## New to C++ and confused by boolean and ifs and else

Hello I'm new To C++ and I have been reading the tutorials I have gotten to the if statment chapter and I'm getting very confused by all the NOT OR ANDs. Is there any way someone could help me out? I have reread the booleans part aleast 5 times and I'm still confused by it. I if anyone could help me out that would be awsome. Thanks for your time! 2. Boolean value is the reult of a comparison or a logical occasion using AND or OR, which can be true of false. C++ used the bool type to represent boolean values. An expression of the type bool can either be true or false, where the internal value got true will be represented as the numerical value 1 and flase by a zero.

Syntax:
Code:
`bool true; // Or false...` 3. If you have an expression 'a || b' you can reconsider it as "if either a or b is true (non-zero)" and if you have an expression 'a && b' then you can reconsider it as "if both a and b are true". When you get to the exercises on this site (which in no way reflect what you need to write in real-life code you will see things like this:

if( (1 || 0) && (1 && 1) )

You can reinterpret is as "if either 1 or 0 are true (non-zero) and if both 1 and 1 are true (non-zero) then execute some code". Since in the first expression, 1 is non-zero and in the second expression both 1's are non-zero, you get to 'if( 1 && 1 )' which basically means "if 1 and 1 are non-zero" which will be evaluated to true because both 1's are non-zero. I hope I didn't confuse you too much =] 4. AND, NOT, OR, XOR, are those same operators you learned in school. I suggested you switch back to those days where this was taught to you in math class. A good source is to search google for "math truth tables".

When you have something like myInt > 12 you have a proposition. Basically this proposition states that "myInt is greater than 12".

Now, propositions have a value. If I say "2 potatoes make a pair of potatoes", i've built a proposition and the value of this proposition is TRUE. Because indeed 2 potatoes make a pair of potatoes.

So, assuming the variable myInt is equal to 17. The proposition above is also true. Because if myInt equals 17, then myInt certainly is greater than 12.

You are now half-way there.

Logical operators in C++ produce logical expressions. What they do is they grab one or more propositions and evaluate them producing one of two possible values. TRUE or FALSE

Let's see...

My car has wheels AND my car can fly. (propositions underlined, logical operator in bold)

Without any math what is the value of that expression. you know that the second proposition is false. So, If I just said that to you your answer would be: "That's a lie!"

The value of that expression is thus false. My car has wheels alright, but it certainly can't fly. The operator AND evaluated my expression to false. True AND False = False.

But what if I had said:

My car has wheels OR my car can fly. (propositions underlined, logical operator in bold)

You answer would probably be "Your car has wheels". You couldn't say that my statement was false. It was true. True OR False = True.

So, taking this to C++... Let's go back to the myInt example:

Code:
```myInt = 17;
myInt > 12 && myInt < 25;  // && means AND in C++
myInt < 12 || myInt < 25;   // || means OR in C++```
What is the value of each of those expressions?
The first is true. The second is true. In order to be able to evaluate you will have to remember about the truth tables. Again, search google for "math truth tables". They will tell you all combinations of true and false and the possible results for each of the logical operators.

An if statement evaluates a logical expression. It is the equivalent of saying: IF myInt > 12 do this, ELSE do that.

Code:
```if( myInt < 12 ) {
// do this
}
else {
// do that
}```
And this is how boolean expressions are evaluated in C++ (if you don't mind the crude explanation).

One last thing is left to be said.

In C++ (as in most other languages) TRUE and FALSE have a value. False evaluates to 0, while True evaluates to everything else.

What exactly does this mean? Well, any value that can be treated as a number can also be treated as a boolean (or logical value). And when that happens, when we want to treat that number has a boolean, any value other than 0 is considered true, while any value equal to 0 is considered false.

So, with our example of myInt above, I could write:

Code:
```if( myInt ) {
// do something here if the variable myInt is not equal to 0
}
else {
// do something here if the variable myInt is equal to 0
}``` 5. Now a test:
It enters the loop or not?
Code:
```// || is for OR
// && is for AND
// ^ is for XOR
int test =23, k=0;
while(((test-20)>(k+3)) ^ ((1 && 0) || true)) {...}```
Solve it without running the code.  7. I'm getting very confused by all the NOT OR ANDs.
This should get easier once you start working on a more realistic program where the variables actually mean something to you:

Examples:
IF there is a blank CD in the drive, AND IF the user clicks "burn"...
IF there is NOT a blank CD in the drive, AND IF the user clicks "burn"...

"IF", "AND", and "OR" have the same meanings in C++ as in English.

We don't use "NOT" in exactly the same way, but it's similar.

We don't say "EXCLUSIVE-OR", we say "One or the other, but not both."

If you are going to become a good programmer, you will have to learn to use Boolean logic more abstractly. But, don't get too bogged-down with it right now.

I have been reading the tutorials... I have reread the booleans part aleast 5 times and I'm still confused by it.
With any technical subject, you are going to have to read the material more than once. (After 5 times, I guess it's time to ask a question or consult another reference..) Consider getting your hands on a beginning C++ book. A beginning book will cover about the same material as the tutorials, but in much more detail, and with much more explanation. For example, Accelerated C++ is about 350 pages, and Teach Yourself C++ in 21 Days is about 700 pages! (See the recommended book post at the top of the forum.) 8. http://computer.howstuffworks.com/boolean.htm
Very good link.   10. Code:
```// || is for OR
// && is for AND
// ^ is for XOR
int test =23, k=0;
while(((test-20)>(k+3)) ^ ((1 && 0) || true)) {...}```
So help me if you ever work on a project with me and give me code like that....you are off the project.   Popular pages Recent additions 