A doubt about infinite loop

Discussion in 'C Programming' started by Anshum Kocher, Sep 25, 2011.

  1. 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?
     
    Anshum Kocher, Sep 25, 2011
    #1
    1. Advertising

  2. On Sep 25, 3:32 pm, Anshum Kocher <> 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.
     
    Harald van Dijk, Sep 25, 2011
    #2
    1. Advertising

  3. 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
     
    Jens Thoms Toerring, Sep 25, 2011
    #3
  4. 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 )
    ()~*~()
    (_)-(_)
    -------------------------------------------------------------------------------
     
    Kleuskes & Moos, Sep 25, 2011
    #4
  5. Anshum Kocher

    Seebs Guest

    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 /
    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.
     
    Seebs, Sep 26, 2011
    #5
  6. Anshum Kocher

    Mark Bluemel Guest

    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.
     
    Mark Bluemel, Sep 27, 2011
    #6
  7. 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...
     
    Kenny McCormack, Sep 27, 2011
    #7
  8. Anshum Kocher

    Phil Carmody Guest

    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
     
    Phil Carmody, Sep 27, 2011
    #8
  9. 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 -
     
    Kenny McCormack, Sep 27, 2011
    #9
  10. Anshum Kocher

    Nobody Guest

    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).
     
    Nobody, Sep 27, 2011
    #10
  11. Anshum Kocher

    Seebs Guest

    On 2011-09-27, Nobody <> 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 /
    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.
     
    Seebs, Sep 27, 2011
    #11
  12. Anshum Kocher

    Seebs Guest

    On 2011-09-27, Mark Bluemel <> 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 /
    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.
     
    Seebs, Sep 27, 2011
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    661
  2. Replies:
    5
    Views:
    626
    benben
    Jan 31, 2006
  3. Replies:
    0
    Views:
    593
  4. Isaac Won
    Replies:
    9
    Views:
    460
    Ulrich Eckhardt
    Mar 4, 2013
  5. Peter Otten
    Replies:
    2
    Views:
    140
    Cousin Stanley
    Aug 10, 2013
Loading...

Share This Page