Thread: if statement confusion

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    83

    if statement confusion

    I have a condition and I would like to instead change the condition so that it would execute the previous statements after else statement.

    It seems hard to explain but I'll try my best to illustrate anyway

    if (condition)
    <statement1>
    else
    <statement2>

    So I would like to change the condition so that

    if (condition)
    <statement2>
    else
    <statement1>

    And the condition is

    Code:
    if(!lightStateAtNextLink || !bIsLightEnAtConnection && nLinkNext.IndexToAttachedNode() != pVeh->_.stAutopilot.m_dwNextNode || bIsLightEnAtConnection && nLinkNext.IndexToAttachedNode() == pVeh->_.stAutopilot.m_dwNextNode)

    And I have changed that to this. Is this correct?

    Code:
    if(lightStateAtNextLink && bIsLightEnAtConnection || nLinkNext.IndexToAttachedNode() == pVeh->_.stAutopilot.m_dwNextNode && !bIsLightEnAtConnection || nLinkNext.IndexToAttachedNode() != pVeh->_.stAutopilot.m_dwNextNode)

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, no. If the first condition is correct then you have to negate the whole thing to flip the result. We end up with:

    Code:
    if( !(!lightStateAtNextLink || !bIsLightEnAtConnection && nLinkNext.IndexToAttachedNode() != pVeh->_.stAutopilot.m_dwNextNode || bIsLightEnAtConnection && nLinkNext.IndexToAttachedNode() == pVeh->_.stAutopilot.m_dwNextNode) )

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No.

    You're not accounting for the fact that && has higher precedence than ||. Because of that it is not just a case of negating individual expressions and swapping && and ||.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Thanks guys!
    How do I go on manipulating individual condition though?

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Not sure what you mean but looking at your other examples you should use parentheses to encapsulate expressions within the overall evaluation
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    1. If you want to invert the condition, you can just put the whole thing in parentheses and add a ! at the beginning.

    2. As mentioned, by DeMorgan's law, inverting a boolean expression is possible if you invert every individual condition, change all || to && and vice versa, AND use parentheses to ensure order of operations is the same as initially.

    So if you wanted to invert 'A && B || C', you could write either:
    !(A && B || C)
    or
    (!A || !B) && !C

    because && has higher precedence than ||, so to keep order of operations the same, parentheses are added.

    3. Really, though, if you're going to calculate this whole thing (including multiple function calls), why not just store the result of the expression in a boolean variable and then just invert it for your second loop? That also means any changes to the expression can be very easily done in one place.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typedef enum statement confusion.
    By Daniel.Blesener in forum C Programming
    Replies: 4
    Last Post: 06-19-2012, 01:57 PM
  2. Dev-C++ 4.9 confusion
    By Waleed Mujeeb in forum C++ Programming
    Replies: 16
    Last Post: 02-27-2012, 11:34 AM
  3. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  4. if,else if and else confusion...
    By diding in forum C Programming
    Replies: 5
    Last Post: 10-10-2004, 08:19 PM
  5. C++ confusion
    By charlie in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2001, 05:46 AM