# Thread: Why does this code produce infinite loop?

1. ## 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. 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. Originally Posted by KBriggs
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. Good question, best if you can find out yourself. Analyze carefully either tracing manually or using debugger.

5. Because you're in undefined behavior territory.

6. 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. @ msh again, Why?just curious.

8. Don't forget strcpy in K&R C.
Code:
``` while( *src++ = *dst++ )    /* definitely OK! */
;
i = ++i;
a[i] = i++;
//  both undefined behaviour.```
sequence point.

9. Originally Posted by msh
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.

10. Originally Posted by Bayint Naung
@ 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. If Compiler is gcc, there is -Wsequence-point flag. Just info.

12. Originally Posted by KBriggs
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. You got it

14. 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. Originally Posted by msh
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.