1. ## Bolean Operators hurt my head. (Trouble understanding) :(

Ok, well recently I took a long break from learning C++. I never did get far,basically understanding pointers, although I did make a pretty useful application, simple but nice.

Anyways, I am learning from step one, and I hit a wall early on. See, I never understood Boolean Operators.

I will copy and paste the main section so you guys don't have to scrounge around.

Boolean operators allow you to create more complex conditional statements. For example, if you wish to check if a variable is both greater than five and less than ten, you could use the boolean AND to ensure both var > 5 and var < 10 are true. In the following discussion of boolean operators, I will capitalize the boolean operators in order to distinguish them from normal english. The actual C++ operators of equivalent function will be described further into the tutorial - the C++ symbols are not: OR, AND, NOT, although they are of equivalent function.

When using if statements, you will often wish to check multiple different conditions. You must understand the Boolean operators OR, NOT, and AND. The boolean operators function in a similar way to the comparison operators: each returns 0 if evaluates to FALSE or 1 if it evaluates to TRUE.

NOT: The NOT operator accepts one input. If that input is TRUE, it returns FALSE, and if that input is FALSE, it returns TRUE. For example, NOT (1) evalutes to 0, and NOT (0) evalutes to 1. NOT (any number but zero) evaluates to 0. In C and C++ NOT is written as !. NOT is evaluated prior to both AND and OR.

AND: This is another important command. AND returns TRUE if both inputs are TRUE (if 'this' AND 'that' are true). (1) AND (0) would evaluate to zero because one of the inputs is false (both must be TRUE for it to evaluate to TRUE). (1) AND (1) evaluates to 1. (any number but 0) AND (0) evaluates to 0. The AND operator is written && in C++. Do not be confused by thinking it checks equality between numbers: it does not. Keep in mind that the AND operator is evaluated before the OR operator.

OR: Very useful is the OR statement! If either (or both) of the two values it checks are TRUE then it returns TRUE. For example, (1) OR (0) evaluates to 1. (0) OR (0) evaluates to 0. The OR is written as || in C++. Those are the pipe characters. On your keyboard, they may look like a stretched colon. On my computer the pipe shares its key with \. Keep in mind that OR will be evaluated after AND.

It is possible to combine several boolean operators in a single statement; often you will find doing so to be of great value when creating complex expressions for if statements. What is !(1 && 0)? Of course, it would be TRUE. It is true is because 1 && 0 evaluates to 0 and !0 evaluates to TRUE (ie, 1).

Try some of these - they're not too hard. If you have questions about them, feel free to stop by our forums.

Code:
```A. !( 1 || 0 )         ANSWER: 0
B. !( 1 || 1 && 0 )    ANSWER: 0 (AND is evaluated before OR)
C. !( ( 1 || 0 ) && 0 )  ANSWER: 1 (Parenthesis are useful)```
None of it just makes sense (Mainly the code part). I was wondering if anyone here could explain it better, or even make a simple program and explain it to me.

I am sorry if my confusion wasn't descriptive enough, but this is just one of those things I totally do not understand. Thanks in advanced for any help provided.

(Also note, I try to keep my grammar and spelling as neat as possible, sorry for any mistakes.)

2. If you know what the words "and", "or", and "not" mean in English, well, then that's it. (Almost: in English, sometimes "or" means "one, or the other, but not both" -- but never in C++.)

The only other thing to remember is 0 = FALSE and number-that-isn't-0 = TRUE. (The output of the logical operators for TRUE will always be 1.)

I never remember the order of operations either, so I always use parentheses. There's probably a list of order of operations in C++ on the web somewhere.

Edit to add: I suppose I could give examples. Say I want to print out all the prime divisors of a number. So, I should print n if it is prime and it is a divisor of d:
Code:
`if (isPrime(n) && isDivisorOf(n,d))`
Say I'm writing a turn-based game; I need to keep going until somebody wins:
Code:
`while (!(PlayerWon || ComputerWon || Tie))`
If the player won or the computer won or it's a tie, I want to stop, so my while loop needs to keep going in the opposite case -- hence the not.

3. since we all speak English.
Think of '&&' as literal 'and', so you are looking for both to be true
And think of '||' as literal 'or'. so you are looking for either one to be true.

4. Originally Posted by tabstop
If you know what the words "and", "or", and "not" mean in English, well, then that's it. (Almost: in English, sometimes "or" means "one, or the other, but not both" -- but never in C++.)

The only other thing to remember is 0 = FALSE and number-that-isn't-0 = TRUE. (The output of the logical operators for TRUE will always be 1.)

I never remember the order of operations either, so I always use parentheses. There's probably a list of order of operations in C++ on the web somewhere.

Edit to add: I suppose I could give examples. Say I want to print out all the prime divisors of a number. So, I should print n if it is prime and it is a divisor of d:
Code:
`if (isPrime(n) && isDivisorOf(n,d))`
Say I'm writing a turn-based game; I need to keep going until somebody wins:
Code:
`while (!(PlayerWon || ComputerWon || Tie))`
If the player won or the computer won or it's a tie, I want to stop, so my while loop needs to keep going in the opposite case -- hence the not.
Ahh, I kind of see what you are saying. I guess the examples they supplied just did not make sense to me.