The problem of changing the value of a variable in a c based for loop

  • 2020-04-02 00:53:30
  • OfStack

Do not know when, very deliberately avoid changing the value of the variable in the for loop.
But sometimes other methods are not convenient to replace, their own try, may find a little difference.
There is no problem with this method:

#include <stdio.h>
int main()
{
 int i;
 for(i=0; i<10; i++)
 {
  i = i+2;
  printf("%d/n", i);
 }
 return 0;
}

But the other method of assignment, it doesn't work.

#include <stdio.h>
int main()
{
 int i;
 for(i=0; i<10; i++)
 {
  i = 2;   //It will go on forever
  printf("%d/n", i);
 }
 return 0;
}

I wonder if this is the only reason why there are many advocates not to change the value of variables in the for loop.

Related articles: