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


Groups > comp.lang.c > #10921 > unrolled thread

A doubt about infinite loop

Started byAnshum Kocher <email.anshum@gmail.com>
First post2011-09-25 06:32 -0700
Last post2011-09-27 22:37 +0000
Articles 12 — 9 participants

Back to article view | Back to comp.lang.c


Contents

  A doubt about infinite loop Anshum Kocher <email.anshum@gmail.com> - 2011-09-25 06:32 -0700
    Re: A doubt about infinite loop Harald van Dijk <truedfx@gmail.com> - 2011-09-25 07:32 -0700
    Re: A doubt about infinite loop jt@toerring.de (Jens Thoms Toerring) - 2011-09-25 14:39 +0000
    Re: A doubt about infinite loop Kleuskes & Moos <kleuske@somewhere.else.net> - 2011-09-25 15:03 +0000
    Re: A doubt about infinite loop Seebs <usenet-nospam@seebs.net> - 2011-09-26 22:59 +0000
      Re: A doubt about infinite loop Mark Bluemel <mark_bluemel@pobox.com> - 2011-09-27 08:40 +0100
        Re: A doubt about infinite loop gazelle@shell.xmission.com (Kenny McCormack) - 2011-09-27 10:35 +0000
        Re: A doubt about infinite loop Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-09-27 14:05 +0300
          Re: A doubt about infinite loop gazelle@shell.xmission.com (Kenny McCormack) - 2011-09-27 12:35 +0000
          Re: A doubt about infinite loop Nobody <nobody@nowhere.com> - 2011-09-27 19:10 +0100
            Re: A doubt about infinite loop Seebs <usenet-nospam@seebs.net> - 2011-09-27 22:37 +0000
        Re: A doubt about infinite loop Seebs <usenet-nospam@seebs.net> - 2011-09-27 22:37 +0000

#10921 — A doubt about infinite loop

FromAnshum Kocher <email.anshum@gmail.com>
Date2011-09-25 06:32 -0700
SubjectA doubt about infinite loop
Message-ID<57b6390c-d172-46a1-8607-fa9d9a917617@k15g2000yqd.googlegroups.com>
 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?

[toc] | [next] | [standalone]


#10922

FromHarald van Dijk <truedfx@gmail.com>
Date2011-09-25 07:32 -0700
Message-ID<e046795d-4753-45a1-a3ec-1fefa0520e40@db5g2000vbb.googlegroups.com>
In reply to#10921
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.

[toc] | [prev] | [next] | [standalone]


#10923

Fromjt@toerring.de (Jens Thoms Toerring)
Date2011-09-25 14:39 +0000
Message-ID<9e8somF9h4U1@mid.uni-berlin.de>
In reply to#10921
Anshum Kocher <email.anshum@gmail.com> 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  ___      jt@toerring.de
   \__________________________      http://toerring.de

[toc] | [prev] | [next] | [standalone]


#10924

FromKleuskes & Moos <kleuske@somewhere.else.net>
Date2011-09-25 15:03 +0000
Message-ID<j5nfrv$8dp$1@dont-email.me>
In reply to#10921
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 )
     ()~*~()   
     (_)-(_)   
-------------------------------------------------------------------------------

[toc] | [prev] | [next] | [standalone]


#10971

FromSeebs <usenet-nospam@seebs.net>
Date2011-09-26 22:59 +0000
Message-ID<slrnj81vel.msp.usenet-nospam@guild.seebs.net>
In reply to#10921
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 <email.anshum@gmail.com> 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-nospam@seebs.net
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.

[toc] | [prev] | [next] | [standalone]


#10999

FromMark Bluemel <mark_bluemel@pobox.com>
Date2011-09-27 08:40 +0100
Message-ID<j5rung$cfp$1@dont-email.me>
In reply to#10971
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.

[toc] | [prev] | [next] | [standalone]


#11004

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2011-09-27 10:35 +0000
Message-ID<j5s8tj$7j2$1@news.xmission.com>
In reply to#10999
In article <j5rung$cfp$1@dont-email.me>,
Mark Bluemel  <mark_bluemel@pobox.com> 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...

[toc] | [prev] | [next] | [standalone]


#11007

FromPhil Carmody <thefatphil_demunged@yahoo.co.uk>
Date2011-09-27 14:05 +0300
Message-ID<87wrcutfd2.fsf@bazspaz.fatphil.org>
In reply to#10999
Mark Bluemel <mark_bluemel@pobox.com> 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

[toc] | [prev] | [next] | [standalone]


#11013

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2011-09-27 12:35 +0000
Message-ID<j5sfu3$hrn$1@news.xmission.com>
In reply to#11007
In article <87wrcutfd2.fsf@bazspaz.fatphil.org>,
Phil Carmody  <thefatphil_demunged@yahoo.co.uk> 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 -

[toc] | [prev] | [next] | [standalone]


#11019

FromNobody <nobody@nowhere.com>
Date2011-09-27 19:10 +0100
Message-ID<pan.2011.09.27.18.10.45.250000@nowhere.com>
In reply to#11007
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).

[toc] | [prev] | [next] | [standalone]


#11029

FromSeebs <usenet-nospam@seebs.net>
Date2011-09-27 22:37 +0000
Message-ID<slrnj84jkr.21l2.usenet-nospam@guild.seebs.net>
In reply to#11019
On 2011-09-27, Nobody <nobody@nowhere.com> wrote:
> It may be far from universal, but it's quite unlikely to be misunderstood.

I've seen it misunderstood several times.  And since it can end up being
used in a case where it implies thinking someone is lying to you when you
actually just aren't sure what they said, it can lead to unpleasantness.

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
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.

[toc] | [prev] | [next] | [standalone]


#11030

FromSeebs <usenet-nospam@seebs.net>
Date2011-09-27 22:37 +0000
Message-ID<slrnj84jie.21l2.usenet-nospam@guild.seebs.net>
In reply to#10999
On 2011-09-27, Mark Bluemel <mark_bluemel@pobox.com> wrote:
> 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.

Not really.

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

That seems likely.

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

When someone's dialect will, unbenknownst to him, cause a billion or so
people to think he speaks English poorly, because they don't know that
dialect, or lead to misunderstandings, I consider it helpful to pass on
the information.

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web