Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #11337

Re: behavior of || operator

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.c
Subject Re: behavior of || operator
Date 2011-10-07 18:47 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <j6nhgo$bm3$1@reader1.panix.com> (permalink)
References <j6nc0d$dii$1@speranza.aioe.org>

Show all headers | View raw


In <j6nc0d$dii$1@speranza.aioe.org> "Mark" <mark_cruzNOTFORSPAM@hotmail.com> 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
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

behavior of || operator "Mark" <mark_cruzNOTFORSPAM@hotmail.com> - 2011-10-07 13:13 -0400
  Re: behavior of || operator James Kuyper <jameskuyper@verizon.net> - 2011-10-07 14:11 -0400
  Re: behavior of || operator John Gordon <gordon@panix.com> - 2011-10-07 18:47 +0000
  Re: behavior of || operator Willem <willem@toad.stack.nl> - 2011-10-07 19:50 +0000
  Re: behavior of || operator Gene <gene.ressler@gmail.com> - 2011-10-08 13:58 -0700

csiph-web