Why does this code produce infinite loop?

This is a discussion on Why does this code produce infinite loop? within the C Programming forums, part of the General Programming Boards category; Hi guys, Im just wondering about the following code: Code: # include <stdio.h> # include <math.h> int main(void) { int ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    5

    Why does this code produce infinite loop?

    Hi guys, Im just wondering about the following code:

    Code:
    # include <stdio.h>
    # include <math.h>
    
    int main(void)
    {
    int i = 0;
    while (i < 10)
    {
    i++;
    if (i = 5) printf("i = %d\n",i);
    }
    return 0;
    }
    when I run it, it produce an infinite loop, can anyone tell me the reasons,why does it produces infinite loop??
    thanks

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    When you say:

    Code:
    if (i = 5)
    you are assigning the value of 5 to i, thus the loop never passes beyond i = 5.

    You need to use == to test if something is equal. Your fixed code would be:

    Code:
    # include <stdio.h>
    # include <math.h>
    
    int main(void)
    {
    int i = 0;
    while (i < 10)
    {
    i++;
    if (i == 5) printf("i = %d\n",i);
    }
    return 0;
    }

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    5
    Quote Originally Posted by KBriggs View Post
    When you say:

    Code:
    if (i = 5)
    you are assigning the value of 5 to i, thus the loop never passes beyond i = 5.
    oo..thank you for answering my questions,

    I have 1 more question to ask regarding about while loop, hope you don't mind
    the code looks something like this:

    Code:
    # include <stdio.h>
    # include <math.h>
    
    int main(void)
    {
    int i = 0, j = 5;
    while (i++ < j--)
    {
    printf("i = %d, j = %d\n", i, j);
    }
    return 0;
    }
    I run the code and I get

    i = 1, j = 4
    i = 2, j = 3
    i = 3, j = 2

    What I don't get is, why its printing out i = 3, j =2 when my code is obviously i < j
    Code:
    while (i++ < j--)

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Good question, best if you can find out yourself. Analyze carefully either tracing manually or using debugger.

  5. #5
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Because you're in undefined behavior territory.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Because you are incrementing i and j in the loop condition, the value of i and j changes before you get to the print statement. On the last run through the loop, you go into it with i=2, j=3. Then since i < j, you enter the loop, but i=3 and j=2 now because of the ++ and -- in the loop test.


    however, you want to be careful using things like that, because to my understanding, not all computers will do the incrementing exactly the same way or in the same order, and you might end up with different output on different machines. (I could be wrong there, but I seem to recall coming across something like this last summer).

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    @ msh again, Why?just curious.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Don't forget strcpy in K&R C.
    Code:
     while( *src++ = *dst++ )    /* definitely OK! */
             ;
      i = ++i;
      a[i] = i++;
    //  both undefined behaviour.
    sequence point.
    Last edited by Bayint Naung; 05-25-2010 at 09:27 AM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by msh View Post
    Because you're in undefined behavior territory.
    Not undefined. Undefined if the same variable was modified several times in the same statement, but obviously it isn't.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Bayint Naung View Post
    @ msh again, Why?just curious.
    I'm fairly certain that there is no way to be be sure that the comparison is performed only after both operands have been incremented/decremented.

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If Compiler is gcc, there is -Wsequence-point flag. Just info.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    5
    Quote Originally Posted by KBriggs View Post
    Because you are incrementing i and j in the loop condition, the value of i and j changes before you get to the print statement. On the last run through the loop, you go into it with i=2, j=3. Then since i < j, you enter the loop, but i=3 and j=2 now because of the ++ and -- in the loop test.


    however, you want to be careful using things like that, because to my understanding, not all computers will do the incrementing exactly the same way or in the same order, and you might end up with different output on different machines. (I could be wrong there, but I seem to recall coming across something like this last summer).
    oh, so the loop is something like, we start of with i=0 and j=5.
    while ( 0 <5) which is true, print out i++ =1 and j--=4,
    then while (1<4) which is true, print out i++ = 2 and j -- = 3,
    then while (2<3) which is true as well, print out i++ = 3 and j-- = 2;
    but while (3 < 2), its false, so it doesn't print out.

    sthg like that?

    Thank you soo much briggs!!! Thanks for answering my question.

  13. #13
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    You got it

  14. #14
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    It's the difference between postfix and prefix increment.
    Compare this
    Code:
    # include <stdio.h>
    # include <math.h>
    
    int main(void)
    {
        int i = 0, j = 5;
        printf("Postfix:\n");
        while (i++ < j--)
        {
            printf("i = %d, j = %d\n", i, j);
        }
    
        i = 0;
        j = 5;
        printf("Prefix:\n");
        while (++i < --j)
        {
            printf("i = %d, j = %d\n", i, j);
        }
    
        return 0;
    }

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by msh View Post
    I'm fairly certain that there is no way to be be sure that the comparison is performed only after both operands have been incremented/decremented.
    Since they are both post-decrement operators, the comparison is definitely performed on the old values.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. Window message loop (Keys)
    By Blackroot in forum Windows Programming
    Replies: 3
    Last Post: 09-12-2006, 05:15 PM
  3. infinite loop error
    By tsubasa in forum C Programming
    Replies: 7
    Last Post: 05-24-2006, 02:37 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. Infinite loop
    By Spectrum48k in forum C++ Programming
    Replies: 9
    Last Post: 01-11-2003, 09:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21