Noob getting confused by booleans

This is a discussion on Noob getting confused by booleans within the C++ Programming forums, part of the General Programming Boards category; Code: A. !( 1 || 0 ) ANSWER: 0 //how is that so? B. !( 1 || 1 && 0 ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    114

    Cool Noob getting confused by booleans

    Code:
    A. !( 1 || 0 )         ANSWER: 0	//how is that so?
    B. !( 1 || 1 && 0 )    ANSWER: 0 (AND is evaluated before OR) //how is that so?
    C. !( ( 1 || 0 ) && 0 )  ANSWER: 1 (Parenthesis are useful)
    In the if statement tutorial i got to the end of it and say these boolean statements and dont under stand how any of those eqaul to false or true
    Cause all i see its saying is Not 1 or 0
    Not 1 or 1 and 0 // By the way does 1 and 0 make it 10 or is the and function like adding ?

    Main question is how is not 1 or 0 eqaul to False When i see no variable around to define wat the number is




    thanks for clearing up the and question
    but wat im trying to ask is how do i no wat the number is in these 3 questions
    Last edited by Nathan the noob; 06-26-2008 at 04:39 AM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Um, I suggest you get reading some more tutorials and such and start figuring out some of the more basic stuff yourself since this is only going to get harder.

    First thing to understand is that boolean expressions in C++ can also be numeric (and indeed are numeric in reality). The idea is that every number is true except 0. So 1 is true, 2 is true, -1 is true, -500 is true, 0 is false. !0 produces 1, and !1 produces 0.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    The rule is: zero is false, non-zero is true.
    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

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The operators && and || are logicial operators, they work on "true or false", that those are the only results you can expect from that. Anything that is not zero is "true", and zero is "false".

    1 && 0 says "is 1 _and also_ 0" both true? 1 && 0 therefore is not true, since 0 is not true.

    The || operator means "either or" (as opposite of the "or" that we use in natural language, which most commonly means "one but not the other", e.g. "Do you want coke or beer?"). So 1 || 0 is true since one of "1 _either or_ 0" is true.

    Human language is not always as logical as computers. I've hear people say "Coffee and tea, anyone?", but they certainly don't mean that we all need to drink both coffee _AND_ tea at the same time (or in sequence, mixed or whatever?), but rather that we have a choice of "coffee" or "tea".

    There is also an order or precedence for all operators:
    http://en.wikipedia.org/wiki/Operato...tor_precedence

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    I've hear people say "Coffee and tea, anyone?", but they certainly don't mean that we all need to drink both coffee _AND_ tea at the same time (or in sequence, mixed or whatever?), but rather that we have a choice of "coffee" or "tea".
    A little off topic, but I know friends who literally like to drink coffee and tea in a mix. When we say "coffee or tea", we usually mean "coffee or tea, but not both" (i.e., exclusive or), but these friends of mine would interpret it as both!
    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

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    I had an old boss (well, a young boss a long time ago) who used to answer all "or" questions" this way, in jest of course:

    "Are we going to Fridays or Outback for lunch?"

    "Yes"

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by medievalelks View Post
    I had an old boss (well, a young boss a long time ago) who used to answer all "or" questions" this way, in jest of course:

    "Are we going to Fridays or Outback for lunch?"

    "Yes"
    I do that too. Particularly if it's a programmer type person that asks.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Nathan,

    I think this is difficult because it's abstract. You may need to think in terms of something being true or false, rather than meaningless boolean 1's & 0's... In real programs, the boolean variables represent the state of something.

    If you can find a book with some real-world examples, it will probably get easier. This is how computer programs "make decisions" and it's a very basic, very important programing concept. If-statements (with boolean logic) and looping (doing stuff over & over) are the two most important concepts in progamming... These are the things that make programming worthwhile.

    If it's lunchtime (lunch = true) AND if I'm hungry (hungry = true), then we are going to Outback for lunch (Outback = true).

    Or, something like this - If there is a blank CD in the drive AND the user clicks "burn", then start burning the CD.

    P.S.
    When you were first learning math, they didn't teach you that 2+2 = 4... They showed you that 2 apples + 2 apples = 4 apples. Now that you understand the concept, you can understand the abstract representation. But in everyday life, you don't use math in an abstract way. The numbers always represent a quantity or "value" of something in particular.
    Last edited by DougDbug; 06-27-2008 at 04:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with array size
    By desmond5 in forum C Programming
    Replies: 4
    Last Post: 12-04-2007, 04:14 PM
  2. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  3. Confused
    By jeev2005 in forum C Programming
    Replies: 5
    Last Post: 06-01-2006, 02:04 PM
  4. Confused
    By (TNT) in forum C# Programming
    Replies: 1
    Last Post: 11-23-2005, 03:49 PM
  5. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM

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