# Help With Boolean Expressions

This is a discussion on Help With Boolean Expressions within the C++ Programming forums, part of the General Programming Boards category; Hi guys I am in need of some help understanding Boolean Expressions. I am a little lost on them and ...

1. ## Help With Boolean Expressions

Hi guys I am in need of some help understanding Boolean Expressions. I am a little lost on them and would like to see if any of you could enlighten me with some tips.

3. I need help understanding Boolean not. The whole true evaluates to false and vice versa is confusing me. So some help understanding how those are used would be helpful.

4. Why does it confuse you? Think of it as a light bulb. If it's on and you flip the switch, it's off. If it's off and you flip the switch, it's on.

5. Then how could it be used?

6. Originally Posted by MikeD
I need help understanding Boolean not. The whole true evaluates to false and vice versa is confusing me.

Is the above statement true or false? If it is false, write a statement to express the truth. You are only allowed to insert the word "not" into the above statement to create this new statement.

7. You are going to feel so bad if he doesn't respond.

Soma

So with the Boolean not operation you take a false statement and then make it true?

9. Originally Posted by MikeD

So with the Boolean not operation you take a false statement and then make it true?
Yes. Beware this only changes the value of the statement, not the value of any variables involved.

Code:
```	if (2+2 == 4) cout << true << endl;
if (!(2+2 == 5)) cout << true << endl;   // same as (2+2 != 5)```
All primitive (non-object) types, including pointers, in C++ can be true or false. Zero and NULL are considered false, everything else is true.

Code:
```	int a = 0, b = 666;
string *s = NULL, *s1 = new string("hey");
if (!a) cout << b << endl;
if (!s) cout << *s1 << endl;```

10. I've tutored students before in symbolic logic, and I can suggest googling links on "propositional logic" or "classical logic" first. There are many more names in use for the fundamental logic that has been studied for centuries.

Boolean algebra is a mathematical systematization of these modes of reasoning. They make sense together, but in my experience, it is best to approach the logic from the ground up.

One of my students who was having problems with logic for his computer courses pulled out a mark in the 90s after going back to basics of logic. He has recommended this book:

Warren Goldfarb - "Deductive Logic"

Again, ignore "deductive" in the title. He has said that the introductory section on logical connectives (AND OR NOT IF->THEN ) was very helpful.

11. Do not think of it as "making" something true. Since we use Boolean expressions in programming to compare things so often, without changing the values themselves, you need to think of it more fundamentally. Think of it as checking, within some context or world (or program), whether some expression, A, describes the state of affairs correctly.

Moving away from the morbid:

Suppose we're in a world or scenario in which you are exactly 6 inches tall. If I were to utter the phrase, "MikeD is 6 inches tall.", my utterance would be TRUE. If I were to utter "Mike D is not 6 inches tall" my utterance would be FALSE. So if we let A stand for the first statement:

A = "MikeD is 6 inches tall."
!A = "MikeD is not 6 inches tall."

There are a number of ways that !A could be true, namely if you were any other height than 6 inches tall. The only way !A can be FALSE is precisely when A is true.

You are not "making" anything false, you are using the basic properties of logic to check conditions, etc. Look up "logical complement"... it is the more general idea behind NOT.

NOTE: As soon as we define the statement A, we have committed to what !A is, and vice versa.

12. Thanks for the help guys, it has cleared things up for me.

13. Originally Posted by MK27
...Zero and NULL are considered false, everything else is true.
I suppose I understand where you are coming from, but I think it would be better to say that a null pointer is considered false. NULL is a null constant (which should not be used nowadays!) which should only be used with pointers, and as such, its meaning by itself should be "undefined."
Indeed, you cannot test whether nullptr is true or false in C++11.

Originally Posted by Ocifer
I've tutored students before in symbolic logic, and I can suggest googling links on "propositional logic" or "classical logic" first. There are many more names in use for the fundamental logic that has been studied for centuries.
That is overkill. Sure, it might be helpful for a software engineer, and someone achieving to be one, but otherwise...

14. Originally Posted by Elysia
Indeed, you cannot test whether nullptr is true or false in C++11.
Can you point to the source of that info ?
It seems possible with my compiler.
Both "!nullptr" and "nullptr==false" tests true.

15. Nevermind. It seems that a nullptr can be converted to false; but never to true, so it's a bit of an oddball.
I cannot get if (!nullptr) to compile, however.

Page 1 of 2 12 Last