Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A doubt about infinite loop

Reply
Thread Tools

A doubt about infinite loop

 
 
Anshum Kocher
Guest
Posts: n/a
 
      09-25-2011
main()
{
unsigned int i=10;
while(i-->=0)
printf("%u ",i);
int a[10];
printf("%d",*a+1-*a+3);


}
This leads to an infinite loop

main()
{
unsigned int i=10;
while(i-=1>=0)
printf("%u ",i);
int a[10];
printf("%d",*a+1-*a+3);



}

But this does not why?
 
Reply With Quote
 
 
 
 
Harald van Dijk
Guest
Posts: n/a
 
      09-25-2011
On Sep 25, 3:32*pm, Anshum Kocher <email.ans...@gmail.com> wrote:
> *main()


int main(void)

> {
> * unsigned int i=10;
> * while(i-->=0)


The condition is equivalent to i>=0, with the side effect of
decrementing i. As i is unsigned, i>=0 is always true.

> * * printf("%u ",i);


Missing #include <stdio.h>

> int a[10];
> * printf("%d",*a+1-*a+3);


I'm not sure what you're trying to do here, and it doesn't seem
relevant to your question.

>
> }
>
> This leads to an infinite loop
>
> *main()
> {
> * unsigned int i=10;
> * while(i-=1>=0)


If you meant while((i-=1)>=0), you would have another infinite loop,
but this means while(i-=(1>=0)). 1>=0 is just a fancy way of writing
1, so it means while(i-=1), or if you prefer while((i-=1)!=0).

> * * printf("%u ",i);
> int a[10];
> * printf("%d",*a+1-*a+3);
>
> }
>
> But this does not why?


And an unsigned int can be 0, so i != 0 is not a useless comparison.
It cannot be negative, so i >= 0 is useless, i >= 0 is always true.
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      09-25-2011
Anshum Kocher <> wrote:
> main()


int main( void )

> {
> unsigned int i=10;
> while(i-->=0)
> printf("%u ",i);
> int a[10];
> printf("%d",*a+1-*a+3);



> }
> This leads to an infinite loop


> main()
> {
> unsigned int i=10;
> while(i-=1>=0)


This could be written much simpler as

while ( i -= 1 )

(note that '-=' has a much lower precedence than '>='
and that '1 >= 0' evaluates to 1) and thus the loop
ends when 'i' has been decremented to 0. If you would
like the same effect as above you'd need

while ( ( i =- 1 ) >= 0 )

Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de
 
Reply With Quote
 
Kleuskes & Moos
Guest
Posts: n/a
 
      09-25-2011
On Sun, 25 Sep 2011 06:32:12 -0700, Anshum Kocher wrote:

> main()
> {
> unsigned int i=10;
> while(i-->=0)
> printf("%u ",i);
> int a[10];
> printf("%d",*a+1-*a+3);
>
>
> }
> This leads to an infinite loop


It should. Can an unsigned integer be negative? If not, why do
you expect that loop to be finite?

> main()
> {
> unsigned int i=10;
> while(i-=1>=0)
> printf("%u ",i);
> int a[10];
> printf("%d",*a+1-*a+3);
> }
>
> But this does not why?


In the second case, the condition isn't what you think it is. Suggestion:
Change "while(i-=1>=0)" to "while((i-=1)>=0)", thus forcing a particular
precedence and see what happens.

More advice: keep boolean expressions and assignments strictly separated,
avoid clever tricks like the plague and be copious with '(' and ')' if
you have _any_ doubts about operator precedence.

-------------------------------------------------------------------------------
_____________________________________
/ They collapsed ... like nuns in the \
\ street ... they had no teen appeal! /
-------------------------------------
\
\
___
{~._.~}
( Y )
()~*~()
(_)-(_)
-------------------------------------------------------------------------------

 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      09-26-2011
In most dialects of English, a "doubt" is what happens when someone
tells you something but you do not believe them. If you want something
explained, what you have is a question or an uncertainty.

On 2011-09-25, Anshum Kocher <> wrote:
> main()


You should declare this as:
int main(void);

> {
> unsigned int i=10;
> while(i-->=0)
> printf("%u ",i);


You should not use printf without #including <stdio.h>.

> int a[10];
> printf("%d",*a+1-*a+3);


You should not refer to uninitialized variables.

> }
> This leads to an infinite loop


It seems as though it would, yes.

> main()
> {
> unsigned int i=10;
> while(i-=1>=0)
> printf("%u ",i);
> int a[10];
> printf("%d",*a+1-*a+3);
> }


> But this does not why?


Parentheses.

What you have written is:
while (i -= (1 >= 0))
...
so we calculate whether 1 >= 0, it is, so (1 >= 0) is 1, and this is
equivalent to
while (i -= 1)
which terminates when i is 0.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      09-27-2011
On 09/26/2011 11:59 PM, Seebs wrote:
> In most dialects of English, a "doubt" is what happens when someone
> tells you something but you do not believe them. If you want something
> explained, what you have is a question or an uncertainty.


I would think that by now native speakers of Indian English have been
posting to UseNet, including this newsgroup, long enough for this
discussion to be dead and buried.

In the dialect of English the OP is familiar with, "doubt" is pretty
much synonymous with "uncertainty".

Given that his query was perfectly intelligible, quibbling about the
difference between his dialect and yours seems petty.

 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      09-27-2011
In article <j5rung$cfp$>,
Mark Bluemel <> wrote:
>On 09/26/2011 11:59 PM, Seebs wrote:
>> In most dialects of English, a "doubt" is what happens when someone
>> tells you something but you do not believe them. If you want something
>> explained, what you have is a question or an uncertainty.

>
>I would think that by now native speakers of Indian English have been
>posting to UseNet, including this newsgroup, long enough for this
>discussion to be dead and buried.
>
>In the dialect of English the OP is familiar with, "doubt" is pretty
>much synonymous with "uncertainty".
>
>Given that his query was perfectly intelligible, quibbling about the
>difference between his dialect and yours seems petty.
>


Welcome to CLC! We hope you enjoy your stay.

--
Just for a change of pace, this sig is *not* an obscure reference to
comp.lang.c...

 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      09-27-2011
Mark Bluemel <> writes:
> On 09/26/2011 11:59 PM, Seebs wrote:
> > In most dialects of English, a "doubt" is what happens when someone
> > tells you something but you do not believe them. If you want something
> > explained, what you have is a question or an uncertainty.

>
> I would think that by now native speakers of Indian English have been
> posting to UseNet, including this newsgroup, long enough for this
> discussion to be dead and buried.
>
> In the dialect of English the OP is familiar with, "doubt" is pretty
> much synonymous with "uncertainty".
>
> Given that his query was perfectly intelligible, quibbling about the
> difference between his dialect and yours seems petty.


But warning the OP that the terminology he's using is far from
universal and likely to be misunderstood by many is helpful.

Phil
--
"Religion is what keeps the poor from murdering the rich."
-- Napoleon
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      09-27-2011
In article <>,
Phil Carmody <> wrote:
....
>But warning the OP that the terminology he's using is far from
>universal and likely to be misunderstood by many is helpful.


Welcome to CLC! We hope you enjoy your stay.

--
Faced with the choice between changing one's mind and proving that there is
no need to do so, almost everyone gets busy on the proof.

- John Kenneth Galbraith -

 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      09-27-2011
On Tue, 27 Sep 2011 14:05:29 +0300, Phil Carmody wrote:

>> Given that his query was perfectly intelligible, quibbling about the
>> difference between his dialect and yours seems petty.

>
> But warning the OP that the terminology he's using is far from
> universal and likely to be misunderstood by many is helpful.


It may be far from universal, but it's quite unlikely to be misunderstood.
I've been seeing that usage for as long as I've been on usenet, and I've
never had any doubt as to the meaning, nor have I seen anyone else who
genuinely didn't understand it (as opposed to people who simply don't like
to be reminded that their dialect isn't actually universal).

 
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
infinite loop unexpectly dies Vedran Vukotic Perl 0 03-02-2006 07:43 AM
loop thru a STL list causes an infinite loop Allerdyce.John@gmail.com C++ 5 01-31-2006 03:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57