May 30, 2021 Article blog
Today, a classmate asked if there was a difference between that i and i in the for loop in the C language, and I told him that there was no difference in the for loop, and now I'm going to make a specific point about the difference between i and i.
Let's start with the while statement:
for(i=1;i<10;i++)int i=0;while (i<10){
printf("www.slyar.com");i++;
}
Write again with the while statement:
for(i=1;i<10;++i)int i=0;while (i<10){
printf("www.slyar.com");++i;
}
As you can see, the value of the last i is 10, so in the for loop, there is no difference between i and i, so what is the difference?
Now let's look at another procedure:
#include<stdio.h>int main(){int i,x;
i=1; x =1; x =i++; // Let X first become the value 1 of i, and then i plus 1printf ("%d", x);
i=1; x =1; x =++i; // Let i plus 1 here, and then X become the value of i 2printf ("%d", x);
system("pause"); r eturn 0; }
Try to run this program and find that the result is 1 2, which is the difference between i and i:
i:: Reference first and then increase
i: Add first and then reference
What exactly does that mean? It is
The current value of i is used in the expression in which i is located, followed by i plus 1
i: Let i add 1 first, and then use the new value of i in the expression in which i is located
I think you should understand that...