Thread: [newb] How is "!(1 && !(0 || 1))" true?

  1. #1
    edd of all trades
    Join Date
    Feb 2006
    Location
    Stafford, England
    Posts
    24

    [newb] How is "!(1 && !(0 || 1))" true?

    Hello there, I've just signed up to this forum after getting confused with the 2nd quiz in the tutorial. First up I just want to say how pleased I am to have found this website, it is really great!

    Years ago I learned how to program quite well in BASIC, i made quite a few games as personal projects. Yesterday I began learning C++ thanks to this site, after downloading Dev-C++

    So far everything is going fine, last night I spent ages on the first tutorial, making notes as I went along, trying the examples, altering them, and basically spending as much time on it as I could. Today I woke up and tried the 2nd, with if statements. This was all a lot more familiar to me from BASIC (apart from using && instead of and etc).
    The only thing that has confused me is a part of the quiz.

    3. Evaluate !(1 && !(0 || 1)).
    A. True
    B. False
    C. Unevaluatable

    Apparently the answer is A, True, but I would appreciate it if someone please explained to me how this is possible?

    When I see that, I read it as:

    NOT(1 AND NOT(0 or 1))

    If something isnt (0 or 1, plus 1), then how can it return true?

    Thanks for any help, I'm just a bit baffled at this, everything else is going swimmingly so far. Cheers!
    Last edited by eddwills; 02-18-2006 at 05:35 AM.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    Go step by step:

    0 OR 1 = 1
    NOT(0 OR 1) = NOT(1) = 0
    1 AND NOT(0 or 1) = 1 AND 0 = 0
    NOT(1 AND NOT(0 or 1)) = NOT(0) = 1

    1 is True

  3. #3
    edd of all trades
    Join Date
    Feb 2006
    Location
    Stafford, England
    Posts
    24

    thanks!

    Ok thankyou, I think I get it now.
    (0 OR 1) = 1?
    I thought it was 0 OR 1, not just 1.

    Surely that is like saying blue or green is green, even though it could be blue.

    I think I do understand you but the logic seems a little... illogical.

  4. #4
    edd of all trades
    Join Date
    Feb 2006
    Location
    Stafford, England
    Posts
    24
    I see! I was just looking at your text saying "1 AND 0 = 0" and though erm..

    But now I see, "TRUE AND FALSE = FALSE"

    Which means "TRUE OR FALSE = TRUE"

    Thanks for helping me clear it up, I just want to make sure I fully understand everything I come across before I let myself move on, I'm really determined to get good at C++.

    Whoopee! Bye for now.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Surely that is like saying blue or green is green, even though it could be blue.
    That analogy is not correct. Here we are dealing with boolean values.
    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

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    Quote Originally Posted by eddwills
    Surely that is like saying blue or green is green, even though it could be blue.
    It's more like saying "sky is green - or - grass is green" which is true because grass really is green. The colour of the sky doesn't make a difference.

  7. #7
    edd of all trades
    Join Date
    Feb 2006
    Location
    Stafford, England
    Posts
    24
    Yup, sorry for that, I understand now though. Thanks again for the help.

  8. #8
    edd of all trades
    Join Date
    Feb 2006
    Location
    Stafford, England
    Posts
    24

    Another question now.

    Hello again!
    I figured making a new thread would've been a bit of a waste of space, so I have another question here.
    It is from the quiz on "Lesson 5: Switch Case".
    4. What is the result of the following code?
    Code:
    x=0;
    
    switch(x)
    
    {
    
      case 1: cout<<"One";
    
      case 0: cout<<"Zero";
    
      case 2: cout<<"Hello World";
    
    }
    A. One
    B. Zero
    C. Hello World
    D. ZeroHello World
    Can someone tell me how when x=0 x manages to fulfil two cases?
    is it because there is no break? I'm guessing it's purposefully bad programming, and should be:
    Code:
    int x=0;
    
    switch(x) {
    
      case 0:
           cout<<"Zero";
           break;
      case 1:
           cout<<"One";
           break;
      case 2:
           cout<<"Hello World";
           break;
    }
    Is this all right? It's not so much of a question, more something I'd like someone with a bit more knowledge to clear up. Obviously having this example is useless alone, as x will only ever equal 0, and also there is no default, but asides from that is everything present ad correct? Thanks a lot

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>which is true because grass really is green

    not where I live -- its been brown now for a few months.

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    You are right, it is because there is no 'break.' The execution "falls through" the rest of the cases. Because most of the time this is not desired you see switch statements with break after each case more often than not.

    There are some uses for it though. For example:
    PHP Code:
    int daysInMonth;

    switch (
    month)
    {
        case 
    1: case 3: case 5: case 7: case 8: case 10: case 12:
            
    daysInMonth 31;
            break;
        case 
    4: case 6: case 9: case 11:
            
    daysInMonth 30;
            break;
        case 
    2:
            
    daysInMonth isLeapYear 29 28;
            break;
        default: 
    /* unknown month?? */


  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    Quote Originally Posted by Ancient Dragon
    >>which is true because grass really is green

    not where I live -- its been brown now for a few months.
    At least you can see it... here I can't check because it's below some vile white stuff

  12. #12
    edd of all trades
    Join Date
    Feb 2006
    Location
    Stafford, England
    Posts
    24
    Hehe, thanks joni. So while the example in the tutorial is bad programming, excluding breaks may be used on purpose for good. C++ is so much better than BASIC! Even things like having different variables to ensure a digit is an integer or whatnot amazed me, I can't wait to get onto more advanced coding. Thanks again for your help, You'll no doubt see me again at some point.

    Bye for now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. && and || operator confusion
    By rohit_second in forum C Programming
    Replies: 8
    Last Post: 09-02-2008, 12:10 AM
  2. Evaluate !(1 && !(0 || 1)).
    By Grumpy_Old_Man in forum C++ Programming
    Replies: 7
    Last Post: 08-18-2003, 01:28 PM
  3. Shorter notation?
    By GaPe in forum C Programming
    Replies: 2
    Last Post: 06-14-2003, 11:35 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM