Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #11332 > unrolled thread
| Started by | "Mark" <mark_cruzNOTFORSPAM@hotmail.com> |
|---|---|
| First post | 2011-10-07 13:13 -0400 |
| Last post | 2011-10-08 13:58 -0700 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.c
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
| From | "Mark" <mark_cruzNOTFORSPAM@hotmail.com> |
|---|---|
| Date | 2011-10-07 13:13 -0400 |
| Subject | behavior of || operator |
| Message-ID | <j6nc0d$dii$1@speranza.aioe.org> |
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.
[toc] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2011-10-07 14:11 -0400 |
| Message-ID | <4E8F40BF.7000804@verizon.net> |
| In reply to | #11332 |
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.
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-10-07 18:47 +0000 |
| Message-ID | <j6nhgo$bm3$1@reader1.panix.com> |
| In reply to | #11332 |
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"
[toc] | [prev] | [next] | [standalone]
| From | Willem <willem@toad.stack.nl> |
|---|---|
| Date | 2011-10-07 19:50 +0000 |
| Message-ID | <slrnj8um0i.tkv.willem@toad.stack.nl> |
| In reply to | #11332 |
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
[toc] | [prev] | [next] | [standalone]
| From | Gene <gene.ressler@gmail.com> |
|---|---|
| Date | 2011-10-08 13:58 -0700 |
| Message-ID | <d847976a-ae24-4e52-aca2-081db9f929c3@z19g2000vby.googlegroups.com> |
| In reply to | #11332 |
On Oct 7, 7:13 pm, "Mark" <mark_cruzNOTFORS...@hotmail.com> 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 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.
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" .
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web