boolean operators

Im having trouble understanding boolean operators, does anyone know a good/easy way to understand them?
1

Seriously though anything except '0' is true, do you have a more specific roadblock? I know that some functions return 0 when there are no errors like your int main() function does and that still trips me up from time to time.
Last edited on
He was asking for boolean operators, not for boolean values. C++ has the following boolean operators:
A && B : the statement is true, when A and B are both true, otherwise it is false
A || B : the statement is true when A or B (or both) are true, otherwise it is false
!A : The statement is true when A is false.

The operators may be combined to create more complicated expressions, for example:
!A && !B : This is true when A and B are both false (equal to !(A || B)
!(A&&B) : This is true when either A or B are false (equal to !A || !B)
(A || B) && (C || D) : this is true when A, B or both are true, and at the same time C,D or both are true
(!A && B) || (A && !B) : this is true when either A or B are true, but false when both are true or both are false

Then there are the following comparison operators:

A == B : true when A is equal to B (note, that doesn't necessarily imply that A IS B)
A != B : true when A is not equal to B
A < B : true when A is smaller than B
A > B : true when A is greater than B
A <= B : true when A is smaller or equal to B
A >= B: true when A is greater or equal to B


Note that types in C++ are allowed to overload those operators, so the actual meaning depends on the type the operators are applied to (some types may not have such operators at all, depending on whether they actually make sense). However, you can normally expect these to behave in a way that makes sense for the type they are implemented.
Last edited on
these are the examples in the tutorial im reading and I cant really understand how they came about the answer

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)

and this is on the quiz

Evaluate !(1 && !(0 || 1))


this is like my second day learning c++, I dont want it to discourage me from learning it because im really interested in learning it and i want to study it in college. I want to learn a bit so that im sure il do well in college
A. !( 1 || 0 ) ANSWER: 0

A. you evaluate what's in the parenthesis first: 1 || 0 will evaluate to 1 (and so is 0 || 1, and 1 || 1), then you negate it thus giving !(1) which is 0; you also can read || as OR and ! as NOT

I'll leave it to you to figure out B and C:
clues: && = AND, and only 1 AND 1 evaluates to 1, others evaluate to 0;
can you tell me if im doing this one right

D. !(1 && !(0 || 1))
= !(1 && !1) (since !A means statemant is true when A is false. in the equation will the !1
turn to !0?)

making it now
!(1 && 0)
!0 = 1 (true)

does that make sense?
Right. 0 || 1 is 1, !1 is 0, 1 && 0 is 0, and !0 is 1. So the result is 1.
sweet, thanks
Topic archived. No new replies allowed.