A doubt about infinite loop

A

Anshum Kocher

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

Harald van Dijk


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 said:
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.
 
J

Jens Thoms Toerring

Anshum Kocher said:

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
 
K

Kleuskes & Moos

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

Seebs

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.


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 said:
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
 
M

Mark Bluemel

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

Kenny McCormack

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

Phil Carmody

Mark Bluemel said:
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
 
K

Kenny McCormack

Phil Carmody said:
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.
 
N

Nobody

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

Seebs

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
 
S

Seebs

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top