Thread: if...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    12

    if...

    its not that I dont get how to use it but i'm just wondering why is it that for example if I had abc as a variable that
    Code:
    if (abc==!4)
    wont work(well it does but if I entered in anything it assumes its false and goes onto the else statement)? since ! takes precedence shouldnt it be read
    not 4 > == > abc and when reconstructed isnt it saying that if abc is equal to not 4 then?
    I tried
    Code:
    if (!(abc==4))
    and it worked but isnt it sortve the same as the if statement before just reworded to if its not equal to 4?
    Rawr.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    !4 equates to 0.
    so the first test is abc == 0

    the second test is completely different.
    First abc == 4
    then "not" the result.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    oh, how does !4 equate to 0 though, i still dont really get it
    Now I have another problem with the loops, is FOR the same as WHILE except you can do more things variable wise with FOR? It seems pretty much the same and I tried adding a loop to:
    Code:
    #include <iostream>
    int main()
    {
        int a;
        std::cout<<"What is 2+2?"<<std::endl;
        std::cin>>a;
        if(!(a==4))
    {
            std::cout<<"That would be correct my fellow mathematician."<<std::endl;
            system ("PAUSE");
    }
        else
    {
            std::cout<<"wrong!"<<std::endl;
            system ("PAUSE");
    }
        return 0;
    }
    so i put
    Code:
    while(!(a==4))
    before cout so it can ask the question again but the question just keeps getting asked and loops over and over, is there a way to make it not do that or would I have to create another variable and add a AND statement to the while code?
    Rawr.

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I don't know if the following is your confusion (just wondering),
    if not then nevermind...
    Code:
    expression (abc==!4)  is equal (!(abc==4))   // eek, wrong
    expression (abc!= 4)  is equal (!(abc==4))   // eek, right
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Make sure that you capture the input in the while loop as well:
    Code:
    while(a != 4)
    {
      std::cout << "...";
      std::cin >> a;
    }

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

    !4

    oh, how does !4 equate to 0 though, i still dont really get it
    Well, think about how if(x) works... if x is non-zero the code following will execute. If x is zero, then the code following will not execute.

    if(4); //Always "true"
    if(!4); // Always "flase" , so the stuff inside the parenthesis must be zero!

    A logical NOT !X is different from a bitwise NOT ~X. This: ~4 does not evaluate to zero.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    here's another way to think about it... when you just put if(constant), it kinda acts like this: if(constant==constant) which it always will, in your case, it would be if(4==4), and in your implementation if(!a), it would be similar to if(!a==a), which it doesn't...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595

    !4

    I like DougDbug's answer. AFAIK, all conditional statements need to be phrased in a true/not-true dichotomy. In C++ zero equates to false (not-true), and any non-zero values equate to true. Therefore if you say:

    if(4)

    then 4 is non-zero so it is interpretted by the compiler as saying if(true). If you say:

    if(!4)

    then the compiler probably first converts the statement to this:

    if(!true)

    and then to this:

    if(false)

    which is the same as this

    if(0)

    so in the end !4 == 0 when used as a conditional. Logically !4 could mean the value could be anything but 4 like -100 or 0 or 3, but when used as a conditional, !4 means 0, given the need for results that equate to either true or not-true.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    I still cant get the loop working but I understand the !4 thing equating to 0 now, thanks, and btw the program is supposed to be a joke type thing where 2+2 equals anything except 4 :P
    Rawr.

  10. #10
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Post the code that you have so far, please
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed