# Thread: Beginer trying to understan the Boolean Operators or,and,not

1. ## Beginer trying to understan the Boolean Operators or,and,not

I am very intrested in learning c++ but, when I am learning needs some explantion along with an example. I figgured that is the best way I can take in things. I started the tutorials last night and was doing great intill I got to the Boolean Operators OR,AND,NOT. I kind of get an idea but tried for awhile to put them in axtion and nothing. The tutorials I am going through can be found here If Statements in C++ - Cprogramming.com Tutorial is there any other online tutorial that will explain this better and give an example or could one of you guys give an example of these. Im not sure if this is a must thing to know in C++ but, I dont want to move on untill I fully understand it. 2. What don't you understand about those operators?

a AND b is true, if both a and b are true.
a OR b is true, if at least one of a and b is true.
NOT is negation, NOT true is false, NOT false is true. 3. There's a bit more than that in C and C++

Firstly, there is short circuit evaluation. An example of short circuit evaluation is when testing x && y. First x is evaluated. If x is false, y is not evaluated, because it not needed to get the final result (false AND anything is always false).

Also, a zero value is false and any non-zero value is true. So "if (42) something();" will always execute something. This works for basic numeric types (bool, int, char, float, etc) as well as for pointers (a zero pointer is the NULL pointer, a special value that does not point at any object).

The rules change a bit for overloaded operators in C++, but that's another story. 4. Would you guys be able to proved an example where this is used so I can play around with it? I tried to aply it to this someway and could not figure it out.

Code:
```#include <iostream>

using namespace std;

int main()                            // Most important part of the program!
{
int age;                            // Need a variable...

cin>> age;                          // The input is put in age
cin.ignore();                       // Throw away enter
if ( age < 100 ) {                  // If the age is less than 100
cout<<"You are pretty young!\n"; // Just to show you it works...
}
else if ( age == 100 ) {            // I use else just to show an example
cout<<"You are old\n";           // Just to show you it works...
}
else {
cout<<"You are really old\n";     // Executed if no other statement is
}
cin.get();
}``` 5. You'd probably have to explain what's wrong with the examples you already have if you want other examples. 6. +1 @tabstop

To the original poster, it's unclear if you're having problems with C++ syntax or the boolean logic itself. To answer one of your side questions, boolean logic is VERY important in programming, so yes it is quite important that you become comfortable with it.

These basic boolean operators accept arguments which can be thought of as statements or propositions (mathematical in nature). It is important that these statements passed as arguments have a clear TRUTH or FALSITY (these are called truth-values). Furthermore, each operator takes the propositions it's been given, and itself returns a truth-value.

AND, OR take two arguments apiece.
NOT takes only one proposition as an argument.

The computer uses 0 to represent FALSITY and non-zero numbers to represent TRUTH.

Code:
```A AND B  , denoted A && B         returns TRUE if both A and B are true
A OR B    , denoted A || B            returns TRUE if one or both of A, B are true

NOT A     , denoted   A  == 0        returns TRUE if A is a FALSITY

//based on the info I've given you, we could denote NOT A differently, and in fact we often use
//another syntax to avoid typing out " == 0 " ... can you work out how?```
An IF statement works on the principle of IF->THEN statements from boolean algebra.

Code:
```if( some_expression ){

// execute something desirable in here

}```
The IF statement will only execute the code in the curly brackets if some_expression evalutes to a non-zero number.

Boolean logic may be studied independently of how computers represent it, and it might be easier for you to try some basic non-computer examples of logic puzzles before implementing it in a program. Such examples can be found by searching "logic" or "proposotional logic" or "propositional calculus" or "boolean" or "LSAT" or something like that on Google. Also, Google "DeMorgan's Law" afterwards... if you can wrap your head around that, you should be able to start using AND, OR, NOT, IF with some confidence. 7. I can give that a try. From what I know I am understanding the c++ but, confused on the boolean logic. Just statred the other night learning this. After the first tutorial it jumped into this right away and was like wooo. Popular pages Recent additions 