Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is the explanation?

3 views
Skip to first unread message

Tadpole

unread,
Feb 27, 2010, 1:16:14 AM2/27/10
to
Hi,
Can anyone explain what is happening?
int x=0;
srand (time(NULL));
for (int i=0;i <100; i++)
{x =rand ()%9;
if ( x !=( 7||8))
printf ("%d ",x);
}

Result of Print
5 8 7 4 8 3 0 7 2 8 2 7 6 7 5 7 8 3 0 0 6 5 0 4 7 6 5 8 5 2 0 2 0 6 4 8 7 3
2 6
2 3 6 2 3 7 2 5 5 6 3 7 2 3 7 4 4 2 5 6 0 4 5 4 4 5 0 5 6 7 5 3 6 3 2 0 5 0
0 5
7 5 8 6 3 2 2 8 3 6 8
Press any key to continue . . .

Question 1 : Why is 1 missing here?


int x=0;
srand (time(NULL));
for (int i=0;i <100; i++)
{x =rand ()%9;
if ( (x != 7) || (x != 8))
printf ("%d ",x);
}

Result of print
3 1 2 8 5 6 2 4 8 2 8 3 1 4 8 6 1 6 7 1 0 4 7 4 0 6 4 8 5 8 2 0 1 1 7 8 2 6
7 7
0 3 1 2 1 6 2 4 1 2 1 8 2 0 2 7 8 8 1 6 0 5 3 1 3 7 3 3 6 5 2 1 2 4 7 3 6 6
3 0
4 2 2 1 4 1 7 1 4 4 3 4 8 0 7 1 2 6 4 7
Press any key to continue . . .

Question 2 : I dont want 7 and 8 to be printed. X is not to be 7 or 8.
But why they are still printed?
What is the correct statement to write?

Thank you
Khoon


Bradley K. Sherman

unread,
Feb 27, 2010, 1:20:59 AM2/27/10
to
In article <4b88b869$1...@news.tm.net.my>, Tadpole <tsk...@streamyx.com> wrote:
>if ( (x != 7) || (x != 8))

&&

--bks

Tadpole

unread,
Feb 27, 2010, 1:36:13 AM2/27/10
to
Thank you Teacher.
I got it


"Bradley K. Sherman" <b...@panix.com> wrote in message
news:hmadkb$9gg$1...@reader1.panix.com...

Richard Delorme

unread,
Feb 27, 2010, 3:03:28 AM2/27/10
to
Le 27/02/2010 07:16, Tadpole a �crit :

[...]


> if ( x !=( 7||8))

> Question 1 : Why is 1 missing here?

The value of (7||8) is 1.

--
Richard

Tadpole

unread,
Feb 27, 2010, 5:50:56 AM2/27/10
to

"Richard Delorme" <abu...@nospam.fr> wrote in message
news:4b88d21a$0$17882$ba4a...@reader.news.orange.fr...

Hi Richard,
Kindly explain why or how (7||8) is 1 ?
I am amazed.

Khoon


santosh

unread,
Feb 27, 2010, 6:07:47 AM2/27/10
to
Tadpole <tsk...@streamyx.com> writes:
> "Richard Delorme" <abu...@nospam.fr> wrote in message
> news:4b88d21a$0$17882$ba4a...@reader.news.orange.fr...
>> Le 27/02/2010 07:16, Tadpole a �crit :
>>
>> [...]
>>> if ( x !=( 7||8))
>>
>>> Question 1 : Why is 1 missing here?
>>
>> The value of (7||8) is 1.

> Hi Richard,


> Kindly explain why or how (7||8) is 1 ?
> I am amazed.

The specs say so is the brief answer:

6.5.14 Logical OR operator
Syntax
1 logical-OR-expression:
logical-AND-expression
logical-OR-expression || logical-AND-expression
Constraints
2 Each of the operands shall have scalar type.

Semantics
3 The || operator shall yield 1 if either of its operands compare
unequal to 0; otherwise, it yields 0. The result has type int.

The && and || operators also perform left to right evaluation and if
the condition is met by the first operand itself, they will not
evaluate the rest of the operands. So in this case the literal 7 is
unequal to zero (C's value for boolean false), so the expression
yields one.


Dominik Zaczkowski

unread,
Feb 27, 2010, 6:12:47 AM2/27/10
to
"Tadpole" <tsk...@streamyx.com> writes:

> "Richard Delorme" <abu...@nospam.fr> wrote in message
> news:4b88d21a$0$17882$ba4a...@reader.news.orange.fr...

>> Le 27/02/2010 07:16, Tadpole a �crit :


>>
>> [...]
>>> if ( x !=( 7||8))
>>
>>> Question 1 : Why is 1 missing here?
>>
>> The value of (7||8) is 1.
>>
>> --
>> Richard
>
> Hi Richard,
> Kindly explain why or how (7||8) is 1 ?
> I am amazed.
>
> Khoon

Any nonzero value in C is treated as true, 7 is nonzero and || operator
yields 1(true) if one of his operands is true. Also remember, that if
first operand is true, then there is no need to evaluate the second.

Seebs

unread,
Feb 27, 2010, 3:11:24 PM2/27/10
to
On 2010-02-27, Tadpole <tsk...@streamyx.com> wrote:
> Kindly explain why or how (7||8) is 1 ?

All non-zero values are true. (x||y) is 1 if either x is true or y is true.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Ersek, Laszlo

unread,
Feb 27, 2010, 4:27:39 PM2/27/10
to
In article <slrnhoivec.1a8...@guild.seebs.net>, Seebs <usenet...@seebs.net> writes:
> On 2010-02-27, Tadpole <tsk...@streamyx.com> wrote:
>> Kindly explain why or how (7||8) is 1 ?
>
> All non-zero values are true. (x||y) is 1 if either x is true or y is true.

... or both are true.

(Or perhaps my English is failing me; in that case, sorry.)

lacos

Seebs

unread,
Feb 27, 2010, 5:12:11 PM2/27/10
to

English in general is failing you; the word "or" is ambiguous.

Willem

unread,
Feb 27, 2010, 5:39:26 PM2/27/10
to
Tadpole wrote:
)
) "Richard Delorme" <abu...@nospam.fr> wrote in message
) news:4b88d21a$0$17882$ba4a...@reader.news.orange.fr...
)> Le 27/02/2010 07:16, Tadpole a ?crit :
)>
)> [...]
)>> if ( x !=( 7||8))
)>
)>> Question 1 : Why is 1 missing here?
)>
)> The value of (7||8) is 1.
)>
)> --
)> Richard
)
) Hi Richard,
) Kindly explain why or how (7||8) is 1 ?
) I am amazed.

What do you expect the value of (7||8) to be ?


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

Scott

unread,
Feb 27, 2010, 10:33:05 PM2/27/10
to
On Sat, 27 Feb 2010 22:39:26 +0000 (UTC), in comp.lang.c, Willem
<wil...@snail.stack.nl> wrote:

>Tadpole wrote:
>)
>) "Richard Delorme" <abu...@nospam.fr> wrote in message
>) news:4b88d21a$0$17882$ba4a...@reader.news.orange.fr...
>)> Le 27/02/2010 07:16, Tadpole a ?crit :
>)>
>)> [...]
>)>> if ( x !=( 7||8))
>)>
>)>> Question 1 : Why is 1 missing here?
>)>
>)> The value of (7||8) is 1.
>)>
>)> --
>)> Richard
>)
>) Hi Richard,
>) Kindly explain why or how (7||8) is 1 ?
>) I am amazed.
>
>What do you expect the value of (7||8) to be ?

I would not expect it to be 1. I would just expect it to not be zero.

Tadpole

unread,
Feb 27, 2010, 11:43:31 PM2/27/10
to
>>)
>>) Hi Richard,
>>) Kindly explain why or how (7||8) is 1 ?
>>) I am amazed.
>>
>>What do you expect the value of (7||8) to be ?
>
> I would not expect it to be 1. I would just expect it to not be zero.

No. The answer is always 1. . I have tested it and it gives me 1 and no
other number.


santosh

unread,
Feb 28, 2010, 1:23:59 AM2/28/10
to
Tadpole <tsk...@streamyx.com> writes:

In such matters of minutiae, it's often better to actually check the
standard rather than check the visible behaviour of your particular
compiler.

But in this case, the standard mandates that the || op yield 1 if
either of it's operands is non-zero. I guess it's restricted to 1,
and not any non-zero value, for consistency and aesthetics.

Willem

unread,
Feb 28, 2010, 5:08:27 AM2/28/10
to
Scott wrote:
) On Sat, 27 Feb 2010 22:39:26 +0000 (UTC), in comp.lang.c, Willem
)<wil...@snail.stack.nl> wrote:
)>What do you expect the value of (7||8) to be ?
)
) I would not expect it to be 1. I would just expect it to not be zero.

Perhaps you should switch to Perl. There, (a||b) is defined as (a?a:b)
(But, obviously, with a only being evaluated once).
And, incidentally, (a&&b) is defined as (a?b:a).

In C, the result of any boolean operator is always 0 or 1.
Dunno why, perhaps because existing compilers did it that way and
code existed that used the result of the boolean for arithmetics.

Nick

unread,
Feb 28, 2010, 5:41:04 AM2/28/10
to
Willem <wil...@snail.stack.nl> writes:

> Scott wrote:
> ) On Sat, 27 Feb 2010 22:39:26 +0000 (UTC), in comp.lang.c, Willem
> )<wil...@snail.stack.nl> wrote:
> )>What do you expect the value of (7||8) to be ?
> )
> ) I would not expect it to be 1. I would just expect it to not be zero.
>
> Perhaps you should switch to Perl. There, (a||b) is defined as (a?a:b)
> (But, obviously, with a only being evaluated once).
> And, incidentally, (a&&b) is defined as (a?b:a).
>
> In C, the result of any boolean operator is always 0 or 1.
> Dunno why, perhaps because existing compilers did it that way and
> code existed that used the result of the boolean for arithmetics.

Spectrum Basic did that well before Perl IIRC. Useful for writing
things like:
PRINT a$ OR 'nothing'
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk

Kaz Kylheku

unread,
Feb 28, 2010, 8:14:17 AM2/28/10
to
On 2010-02-28, Nick <3-no...@temporary-address.org.uk> wrote:
> Spectrum Basic did that well before Perl IIRC. Useful for writing
> things like:
> PRINT a$ OR 'nothing'

Lisp!

(or a1 a2 a3 ... an)

The arguments ai are evaluated in order, stopping at the first one which
yields true (a value other than nil), in which case that value is the
result. Otherwise, the result is nil.

This feature is described in the Lisp 1.5 manual whose preface is dated
August 17, 1962. (Appendix A, Functions and Constants in the LISP
System).

Scott

unread,
Feb 28, 2010, 11:47:18 AM2/28/10
to
On Sun, 28 Feb 2010 10:08:27 +0000 (UTC), in comp.lang.c, Willem
<wil...@snail.stack.nl> wrote:

>Scott wrote:
>) On Sat, 27 Feb 2010 22:39:26 +0000 (UTC), in comp.lang.c, Willem
>)<wil...@snail.stack.nl> wrote:
>)>What do you expect the value of (7||8) to be ?
>)
>) I would not expect it to be 1. I would just expect it to not be zero.
>
>Perhaps you should switch to Perl.

Eh. No. Just...no.

>In C, the result of any boolean operator is always 0 or 1.
>Dunno why, perhaps because existing compilers did it that way and
>code existed that used the result of the boolean for arithmetics.

Yes, yet writing C according to my stated assumption above still yeilds
semantically correct code. And it keeps me in the right frame of mind,
without having to stop to think about it, to handle languages where
"true==1" isn't necessarily so, ultimately producing fewer bugs.

Seebs

unread,
Feb 28, 2010, 12:33:17 PM2/28/10
to
On 2010-02-28, Willem <wil...@snail.stack.nl> wrote:
> Perhaps you should switch to Perl. There, (a||b) is defined as (a?a:b)
> (But, obviously, with a only being evaluated once).
> And, incidentally, (a&&b) is defined as (a?b:a).

Ruby is the same way, at least with ||.

> In C, the result of any boolean operator is always 0 or 1.

PHP is the same way, at least with ||.

... Note that if you happen to be doing two projects, both web based, one in
Ruby and one in PHP, this is a particularly irritating difference.

Although it's an extension, GNU C has a very nice way to express that:
a ?: b

I actually sorta wish that ISO C would pick that one up, it's very expressive.

Willem

unread,
Feb 28, 2010, 1:01:03 PM2/28/10
to
Scott wrote:
) On Sun, 28 Feb 2010 10:08:27 +0000 (UTC), in comp.lang.c, Willem
)<wil...@snail.stack.nl> wrote:
)
)>Scott wrote:
)>) On Sat, 27 Feb 2010 22:39:26 +0000 (UTC), in comp.lang.c, Willem
)>)<wil...@snail.stack.nl> wrote:
)>)>What do you expect the value of (7||8) to be ?

)>)
)>) I would not expect it to be 1. I would just expect it to not be zero.
)>
)>Perhaps you should switch to Perl.
)
) Eh. No. Just...no.
)
)>In C, the result of any boolean operator is always 0 or 1.
)>Dunno why, perhaps because existing compilers did it that way and
)>code existed that used the result of the boolean for arithmetics.
)
) Yes, yet writing C according to my stated assumption above still yeilds
) semantically correct code. And it keeps me in the right frame of mind,
) without having to stop to think about it, to handle languages where
) "true==1" isn't necessarily so, ultimately producing fewer bugs.

I was under the mistaken impression that I was talking to the same person
who was amazed that (7||8) yielded 1. You know, the one that my question
was directed to. Of course, I should have checked the name. My apology.

pete

unread,
Feb 28, 2010, 10:10:34 PM2/28/10
to
Scott wrote:

> Yes, yet writing C according to my stated assumption above
> still yeilds semantically correct code.
> And it keeps me in the right frame of mind,
> without having to stop to think about it, to handle languages where
> "true==1" isn't necessarily so, ultimately producing fewer bugs.

In C, it's better to think of it this way: (true != 0 && false == 0)

--
pete

Kenneth Brody

unread,
Mar 1, 2010, 12:10:06 PM3/1/10
to
On 2/27/2010 1:16 AM, Tadpole wrote:
[...]

> if ( (x != 7) || (x != 8))
> printf ("%d ",x);
[...]

> Question 2 : I dont want 7 and 8 to be printed. X is not to be 7 or 8.
> But why they are still printed?

Because 8 it not equal to 7, and 7 is not equal to 8. It is not possible
for "x != 7" and "x != 8" to both be false.

> What is the correct statement to write?

if ( (x != 7) && (x != 8) )


To a computer, the concepts of "and" and "or" are not exactly the same as in
spoken human communication.

Consider "show me a list of clients living in New York and New Jersey". If
you tell a computer:

if ( state == state_NY && state == state_NJ )

you're not going to get very many hits, because the state cannot be both New
York _and_ New Jersey at the same time.

The "correct" way to ask is "show me a list of clients whose address is
either New York _or_ New Jersey":

if ( state == state_NY || state == state_NJ )

--
Kenneth Brody

osmium

unread,
Mar 1, 2010, 12:17:58 PM3/1/10
to
Kenneth Brody wrote:

Which ignores the point made upstream that the English language is ambiguous
WRT the word "or".

Do you want a Coke or a Pepsi?


Richard Bos

unread,
Mar 1, 2010, 4:14:29 PM3/1/10
to
Nick <3-no...@temporary-address.org.uk> wrote:

> Spectrum Basic did that well before Perl IIRC. Useful for writing
> things like:
> PRINT a$ OR 'nothing'

YDNRC. That statement is illegal in Speccy BASIC, and even
PRINT a OR 4
prints 1 regardless of the value of a.

Richard

Richard Bos

unread,
Mar 1, 2010, 4:35:34 PM3/1/10
to
"osmium" <r124c...@comcast.net> wrote:

> Kenneth Brody wrote:
>
> > To a computer, the concepts of "and" and "or" are not exactly the
> > same as in spoken human communication.

> Which ignores the point made upstream that the English language is ambiguous

> WRT the word "or".
>
> Do you want a Coke or a Pepsi?

No. I agree with my computer on this.

Richard

Nick

unread,
Mar 1, 2010, 5:33:38 PM3/1/10
to
ral...@xs4all.nl (Richard Bos) writes:

Hmm. QL Basic then? I certainly remember it from some Sinclair
programming.

Michael Foukarakis

unread,
Mar 2, 2010, 6:28:25 AM3/2/10
to

No it doesn't, no it isn't, and !(x == a || x == b) is equivalent to
(x != a && x != b).

> Do you want a Coke or a Pepsi?

Yes. I want a Coke, or a Pepsi. Make haste.

Kenneth Brody

unread,
Mar 3, 2010, 2:58:00 PM3/3/10
to


Q: Do you know if the baby is going to be a boy or a girl?
A1: Yes.
A2: Well, I would certainly hope it is.


Q: Do you know what time it is?
A: [looks at watch] Yes.

--
Kenneth Brody

Phil Carmody

unread,
Mar 5, 2010, 3:46:12 AM3/5/10
to

Wrong thing to do. Testing proves nothing. Only looking at the
language definition tells what you should expect from a language
contruct. Testing just tells you if a particular implementation
follows that standard in that particular case.

Phil
--
I find the easiest thing to do is to k/f myself and just troll away
-- David Melville on r.a.s.f1

Richard Bos

unread,
Mar 10, 2010, 9:14:33 AM3/10/10
to
Nick <3-no...@temporary-address.org.uk> wrote:

> ral...@xs4all.nl (Richard Bos) writes:
>
> > Nick <3-no...@temporary-address.org.uk> wrote:
> >
> >> Spectrum Basic did that well before Perl IIRC. Useful for writing
> >> things like:
> >> PRINT a$ OR 'nothing'
> >
> > YDNRC. That statement is illegal in Speccy BASIC, and even
> > PRINT a OR 4
> > prints 1 regardless of the value of a.
>
> Hmm. QL Basic then? I certainly remember it from some Sinclair
> programming.

Possibly. The QL manual isn't clear (it only talks of "true" and
"false", never of their numerical values). However, one (not entirely
default) emulator seems to think not.

Richard

Nick

unread,
Mar 10, 2010, 1:19:27 PM3/10/10
to
ral...@xs4all.nl (Richard Bos) writes:

I'm now baffled. I came across it many years ago, and can't think where
else it might be. It seems to be a Python idiom, but my experience was
a long time before that. I liked it so much I made my own language do
it. Most odd.

Phil Carmody

unread,
Mar 11, 2010, 6:53:30 PM3/11/10
to
Nick <3-no...@temporary-address.org.uk> writes:
> ral...@xs4all.nl (Richard Bos) writes:
>> Nick <3-no...@temporary-address.org.uk> wrote:
>>> ral...@xs4all.nl (Richard Bos) writes:
>>> > Nick <3-no...@temporary-address.org.uk> wrote:
>>> >
>>> >> Spectrum Basic did that well before Perl IIRC. Useful for writing
>>> >> things like:
>>> >> PRINT a$ OR 'nothing'
>>> >
>>> > YDNRC. That statement is illegal in Speccy BASIC, and even
>>> > PRINT a OR 4
>>> > prints 1 regardless of the value of a.
>>>
>>> Hmm. QL Basic then? I certainly remember it from some Sinclair
>>> programming.
>>
>> Possibly. The QL manual isn't clear (it only talks of "true" and
>> "false", never of their numerical values). However, one (not entirely
>> default) emulator seems to think not.
>
> I'm now baffled. I came across it many years ago, and can't think where
> else it might be. It seems to be a Python idiom, but my experience was
> a long time before that. I liked it so much I made my own language do
> it. Most odd.

Perl's older than python by quite a way. Is there any chance you're
thinking of that?

Keith Thompson

unread,
Mar 11, 2010, 7:42:37 PM3/11/10
to

Using "||" or "or" for control flow is a common Perl idiom:

open $FILE, '<', "foo.txt" or die "foo.txt: $!\n";

which is equivalent to:

if (not open $FILE, '<', "foo.txt") {
die "foo.txt: $!\n";
}

And Perl got it from Bourne shell programming.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Dave Hansen

unread,
Mar 12, 2010, 10:46:25 AM3/12/10
to
On Mar 11, 5:53 pm, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
wrote:

> Perl's older than python by quite a way. Is there any chance you're
> thinking of that?

For sufficiently small values of "quite a way" (Perl: 1987, Python:
1989).

Regards,

-=Dave

Nick

unread,
Mar 13, 2010, 3:52:36 AM3/13/10
to
Phil Carmody <thefatphi...@yahoo.co.uk> writes:

Definitely pre-Perl. I've never seriously done any Perl - and certainly
not enough to have picked this up. And I certainly didn't invent it.

0 new messages