Thread: Help With Boolean Expressions

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    5

    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.

    Thanks in advanced.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    What's your question?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    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. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    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.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Then how could it be used?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MikeD
    I need help understanding Boolean not. The whole true evaluates to false and vice versa is confusing me.
    MikeD is dead.

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    You are going to feel so bad if he doesn't respond.

    Soma

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    MikeD is not dead

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

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MikeD View Post
    MikeD is not dead

    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;
    Last edited by MK27; 02-23-2012 at 06:39 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    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.
    Last edited by Ocifer; 02-23-2012 at 11:09 AM.
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    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.
    Last edited by Ocifer; 02-23-2012 at 11:24 AM.
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Thanks for the help guys, it has cleared things up for me.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MK27 View Post
    ...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.

    Quote Originally Posted by Ocifer View Post
    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...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    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. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expressions
    By DMEngOba in forum C Programming
    Replies: 2
    Last Post: 05-25-2009, 03:56 PM
  2. shortcutting in boolean expressions?
    By cyberfish in forum C++ Programming
    Replies: 17
    Last Post: 02-14-2008, 06:39 PM
  3. Liitle help with boolean expressions
    By Voyd in forum C++ Programming
    Replies: 2
    Last Post: 05-30-2006, 08:17 PM
  4. Replies: 4
    Last Post: 04-02-2004, 07:30 PM
  5. expressions
    By lizardking3 in forum Tech Board
    Replies: 5
    Last Post: 02-18-2003, 10:41 AM