behavior of || operator

Discussion in 'C Programming' started by Mark, Oct 7, 2011.

  1. Mark

    Mark Guest

    Hello,

    #define ATTACH 1
    #define DETACH 2

    #define DIRECTION_INGRESS 1 /* Incoming */
    #define DIRECTION_EGRESS 2 /* Outgoing */



    if ((action != ATTACH) || (action != DETACH))

    puts("invalid action");

    if ((dir != DIRECTION_INGRESS) || (dir != DIRECTION_EGRESS))
    puts("invalid direction");



    I appears that condition within if() is verified for being TRUE or FALSE,
    and it is sufficient to have just one operand of || operator to return TRUE
    in order for the whole expression yield TRUE result.



    For example, if action = 1, then (action != ATTACH) becomes FALSE, then
    (action != DETACH) is evaluated and it will return TRUE, thus making the
    statement within if() TRUE. And this is wrong behavior, given that I need to
    have actoni be either 1 or 2.



    To make it right, || must be replaced with &&.



    Is my reasoing above correct and valid?



    Thanks.



    Mark.
     
    Mark, Oct 7, 2011
    #1
    1. Advertising

  2. Mark

    Stefan Ram Guest

    "Mark" <> writes:
    >I appears that condition within if() is verified for being TRUE or FALSE,


    The if-expression is /evaluated/. The first substatement
    is executed if the expression compares unequal to 0.

    The || operator yields 1 if either of its operands compare
    unequal to 0; otherwise, it yields 0.
     
    Stefan Ram, Oct 7, 2011
    #2
    1. Advertising

  3. Mark

    James Kuyper Guest

    On 10/07/2011 01:13 PM, Mark wrote:
    > Hello,
    >
    > #define ATTACH 1
    > #define DETACH 2
    >
    > #define DIRECTION_INGRESS 1 /* Incoming */
    > #define DIRECTION_EGRESS 2 /* Outgoing */
    >
    >
    >
    > if ((action != ATTACH) || (action != DETACH))
    >
    > puts("invalid action");


    This code is equivalent to:

    if(action != ATTACH)
    puts("invalid action");
    else if(action != DETACH)
    puts("invalid action");

    Since ATTACH != DETACH, this is, in turn equivalent to:
    puts("invalid action");

    I don't think that's what you want to do.

    > if ((dir != DIRECTION_INGRESS) || (dir != DIRECTION_EGRESS))
    > puts("invalid direction");
    >
    >
    >
    > I appears that condition within if() is verified for being TRUE or FALSE,
    > and it is sufficient to have just one operand of || operator to return TRUE
    > in order for the whole expression yield TRUE result.


    Yes, that's what || means: it either operand is true, the whole
    expression is true.

    > For example, if action = 1, then (action != ATTACH) becomes FALSE, then
    > (action != DETACH) is evaluated and it will return TRUE, thus making the
    > statement within if() TRUE. And this is wrong behavior, given that I need to
    > have actoni be either 1 or 2.
    >
    >
    >
    > To make it right, || must be replaced with &&.


    Yes:
    if ((action != ATTACH) && (action != DETACH))
    puts("invalid action");

    is equivalent to:

    if(action != ATTACH)
    {
    if(action != DETACH)
    puts("invalid action");
    }

    Which seems a better fit to what I think you want.
     
    James Kuyper, Oct 7, 2011
    #3
  4. Mark

    John Gordon Guest

    In <j6nc0d$dii$> "Mark" <> writes:

    > #define ATTACH 1
    > #define DETACH 2


    > if ((action != ATTACH) || (action != DETACH))
    > puts("invalid action");


    > I appears that condition within if() is verified for being TRUE or FALSE,
    > and it is sufficient to have just one operand of || operator to return TRUE
    > in order for the whole expression yield TRUE result.


    > To make it right, || must be replaced with &&.


    > Is my reasoing above correct and valid?


    You should use && instead of ||, yes.

    But not because of the behavior you describe above; using || is simply
    incorrect logic.

    No matter what value action has, it will ALWAYS be either unequal to 1 or
    unequal to 2. Using || is just wrong. You want to know when it is unequal
    to 1 AND unequal to 2.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Oct 7, 2011
    #4
  5. Mark

    Willem Guest

    Mark wrote:
    ) if ((action != ATTACH) || (action != DETACH))
    ) puts("invalid action");

    It looks like you don't understand De Morgan's laws.

    In English:
    If action is not attach or action is not detach, then it is an invalid
    action.

    What you wanted to do is this:
    If action is not attach or detach, then it is an invalid action.

    Which is:
    if (!(action == ATTACH || action == DETACH))
    puts("invalid action");

    Which, according to De Morgan, is equivalent to:
    if ((action != ATTACH) && (action != DETACH))
    puts("invalid action");


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Oct 7, 2011
    #5
  6. Mark

    Gene Guest

    On Oct 7, 7:13 pm, "Mark" <> wrote:
    > Hello,
    >
    > #define ATTACH           1
    > #define DETACH           2
    >
    > #define DIRECTION_INGRESS       1   /* Incoming */
    > #define DIRECTION_EGRESS        2   /* Outgoing */
    >
    > if ((action != ATTACH) || (action != DETACH))
    >
    >     puts("invalid action");
    >
    > if ((dir != DIRECTION_INGRESS) || (dir != DIRECTION_EGRESS))
    >     puts("invalid direction");
    >
    > I appears that condition within if() is verified for being TRUE or FALSE,
    > and it is sufficient to have just one operand of || operator to return TRUE
    > in order for the whole expression yield TRUE result.
    >
    > For example, if action = 1, then (action != ATTACH) becomes FALSE, then
    > (action != DETACH) is evaluated and it will return TRUE, thus making the
    > statement within if() TRUE. And this is wrong behavior, given that I needto
    > have actoni be either 1 or 2.
    >
    > To make it right, || must be replaced with &&.
    >
    > Is my reasoing above correct and valid?
    >
    > Thanks.
    >
    > Mark.


    I think you'll want to read up on De Morgan's laws (and all the rest
    of simple Boolean logic, which BTW is normally part of high school
    algebra).

    If you have a condition "p or q" and want to logically invert it, the
    rule is not(p or q) = not p and not q.

    In this case I think you're saying "if dir is not equal to either
    INGRESS or EGRESS, then it's an error". By De Morgan, this translates
    to if "dir is not INGRESS _and_ dir is not EGRESS, it's an error" .
     
    Gene, Oct 8, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karlo Basic

    Unusual operator behavior

    Karlo Basic, Nov 10, 2003, in forum: C++
    Replies:
    11
    Views:
    577
    Karl Heinz Buchegger
    Nov 13, 2003
  2. Mantorok Redgormor
    Replies:
    70
    Views:
    1,773
    Dan Pop
    Feb 17, 2004
  3. The behavior of the operator %

    , Mar 18, 2005, in forum: C Programming
    Replies:
    8
    Views:
    285
    Richard Bos
    Mar 21, 2005
  4. noone
    Replies:
    5
    Views:
    315
    noone
    Jan 26, 2007
  5. Replies:
    4
    Views:
    539
Loading...

Share This Page