Thread: if statements?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    if statements?

    whats up everyone,
    It's your friendly neighborhood chap in again for some crazy tales of danger...well not really...just another question to make you roll your eyes in disgust, haha. So in this book I'm reading there's an example of an if statement that says:
    Code:
    if (x%7 == 0 && y%7==0)
         cout << "There's a mushroom here.\n";
    and then a few lines later in the book the author says that the following code would have the exact same effect:

    Code:
    if ( !(x%7)  &&  !(y%7) )
        cout << "There's a mushroom here.\n";

    Now I thought I had a firm grasp on many concepts...but I'm not understanding how the 2 if statements mean the same thing...any insight into this? Chap =

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    if x%7 == 0 wouldn't !(x%7) mean not 0 as well thus being making the 2 statements exact opposites of each other? Just my thinking...

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    ! basically flips a value.

    For example:

    !0 == 1

    !1 == 0

    !500 == 0

    any nonzero value that is not'ed is zero, and zero not'ed is always non-zero (I believe it should always be 1, technically).

    So, if you do:

    !(5%7)

    and 5%7 is 2, then you have

    !(2)

    which is 0, or false

    also the same as if 2==0

    which it doesn't. false.

    *points and laughs at Hunter2 and stovellp*
    vvvvvvvvvvvvvvvvvvvvvvvv
    Last edited by jverkoey; 03-07-2005 at 11:02 PM.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Anything other than 0 is interpreted as true, and 0 is interpreted as false. Therefore:
    If x % 7 != 0 then:
    x % 7 == 'true'

    If x % 7 == 0 then:
    x % 7 == 'false'

    Logically then, where x % 7 == 0, x % 7 is 'false', so !(x % 7) is true.

    **EDIT**
    Beaten
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    x%7 == 0 divides x by seven and gives a remainder of 0.
    0 == 0 is true, so the statement is executed.

    !(x%7) also divides x by seven, and and gives a remainder of 0.
    !0 is the same as 0 == 0, and this the statement is again executed.

    Put it in your compiler and you'll see what I mean.

    I hate using "!()", because it confuses people for exactly this reason. Replace "!(..)" with "== 0" everywhere you see it, and it'll make much more sense.

    Edit: ahh you both beat me.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    haha I completely overlooked that minor obvious detail and realized why that statement meant the same...I was about to write a post saying so and then you 2 came along and made me look bad...damn you! haha thanks for the insight though...any extra words of wisdom are always appreciated...the drinks are on me tonight!!

    ***
    Edit: ahem...you THREE came along grrr

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Quote Originally Posted by Chaplin27
    the drinks are on me tonight
    I'm going to hold you to that. >.<

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    Quote Originally Posted by jverkoey
    I'm going to hold you to that. >.<
    haha...damn, you ever make an offer so you look like the nice guy and then someone actually goes against the unwritten law of never accepting such offers and actually takes you up on it? Well ummm...good...me either...sigh...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. C++ If Statements Help
    By moporho in forum C++ Programming
    Replies: 19
    Last Post: 01-18-2008, 08:40 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Need help with "if" statements
    By Harryt123 in forum C Programming
    Replies: 22
    Last Post: 05-14-2006, 08:18 AM