Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > What is the explanation?

Reply
Thread Tools

What is the explanation?

 
 
Tadpole
Guest
Posts: n/a
 
      02-27-2010
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||)
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 != )
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


 
Reply With Quote
 
 
 
 
Bradley K. Sherman
Guest
Posts: n/a
 
      02-27-2010
In article <4b88b869$(E-Mail Removed)>, Tadpole <(E-Mail Removed)> wrote:
>if ( (x != 7) || (x != )


&&

--bks

 
Reply With Quote
 
 
 
 
Tadpole
Guest
Posts: n/a
 
      02-27-2010
Thank you Teacher.
I got it


"Bradley K. Sherman" <(E-Mail Removed)> wrote in message
news:hmadkb$9gg$(E-Mail Removed)...
> In article <4b88b869$(E-Mail Removed)>, Tadpole <(E-Mail Removed)>
> wrote:
>>if ( (x != 7) || (x != )

>
> &&
>
> --bks
>



 
Reply With Quote
 
Richard Delorme
Guest
Posts: n/a
 
      02-27-2010
Le 27/02/2010 07:16, Tadpole a écrit :

[...]
> if ( x !=( 7||)


> Question 1 : Why is 1 missing here?


The value of (7|| is 1.

--
Richard
 
Reply With Quote
 
Tadpole
Guest
Posts: n/a
 
      02-27-2010

"Richard Delorme" <(E-Mail Removed)> wrote in message
news:4b88d21a$0$17882$(E-Mail Removed). fr...
> Le 27/02/2010 07:16, Tadpole a écrit :
>
> [...]
>> if ( x !=( 7||)

>
>> Question 1 : Why is 1 missing here?

>
> The value of (7|| is 1.
>
> --
> Richard


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

Khoon


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-27-2010
Tadpole <(E-Mail Removed)> writes:
> "Richard Delorme" <(E-Mail Removed)> wrote in message
> news:4b88d21a$0$17882$(E-Mail Removed). fr...
>> Le 27/02/2010 07:16, Tadpole a �crit :
>>
>> [...]
>>> if ( x !=( 7||)

>>
>>> Question 1 : Why is 1 missing here?

>>
>> The value of (7|| is 1.


> Hi Richard,
> Kindly explain why or how (7|| 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.


 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      02-27-2010
On 2010-02-27, Tadpole <(E-Mail Removed)> wrote:
> Kindly explain why or how (7|| 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 / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Ersek, Laszlo
Guest
Posts: n/a
 
      02-27-2010
In article <(E-Mail Removed)>, Seebs <(E-Mail Removed)> writes:
> On 2010-02-27, Tadpole <(E-Mail Removed)> wrote:
>> Kindly explain why or how (7|| 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
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      02-27-2010
On 2010-02-27, Ersek, Laszlo <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)>, Seebs <(E-Mail Removed)> writes:
>> On 2010-02-27, Tadpole <(E-Mail Removed)> wrote:
>>> Kindly explain why or how (7|| 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.)


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

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      02-27-2010
Tadpole wrote:
)
) "Richard Delorme" <(E-Mail Removed)> wrote in message
) news:4b88d21a$0$17882$(E-Mail Removed). fr...
)> Le 27/02/2010 07:16, Tadpole a ?crit :
)>
)> [...]
)>> if ( x !=( 7||)
)>
)>> Question 1 : Why is 1 missing here?
)>
)> The value of (7|| is 1.
)>
)> --
)> Richard
)
) Hi Richard,
) Kindly explain why or how (7|| is 1 ?
) I am amazed.

What do you expect the value of (7|| 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments