Like Tree5Likes

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. #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
    1,625
    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
    1,625
    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
    22,308
    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.
    GReaper likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    4,415
    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
    83
    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
    83
    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
    Posts
    23,031
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    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.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    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.

Page 1 of 2 12 LastLast
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, 04: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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21