Velocity Reviews > A doubt about infinite loop

# 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?

Harald van Dĳk
Guest
Posts: n/a

 09-25-2011
On Sep 25, 3:32*pm, Anshum Kocher <(E-Mail Removed)> 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.

Jens Thoms Toerring
Guest
Posts: n/a

 09-25-2011
Anshum Kocher <(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de

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 )
()~*~()
(_)-(_)
-------------------------------------------------------------------------------

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 <(E-Mail Removed)> 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 / (E-Mail Removed)
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.

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.

Kenny McCormack
Guest
Posts: n/a

 09-27-2011
In article <j5rung\$cfp\$(E-Mail Removed)>,
Mark Bluemel <(E-Mail Removed)> 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...

Phil Carmody
Guest
Posts: n/a

 09-27-2011
Mark Bluemel <(E-Mail Removed)> 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

Kenny McCormack
Guest
Posts: n/a

 09-27-2011
In article <(E-Mail Removed)>,
Phil Carmody <(E-Mail Removed)> 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 -

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).