有人可以解釋這兩個for循環代碼之間的區別嗎?C - for循環代碼之間的區別
代碼1:
int j = 0;
int i;
int n = 8;
for (i = 0; i < n; i++){
printf("Value if I: %d\n",i);
while (j < n){
printf("Value of J: %d\n",j);
j++;
}
}
}
OUTPUT:
i = 0 , j = 0,1,2,3,4,5,6,7
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
代碼2:
int i;
int n = 8;
for (i = 0; i < n; i++){
printf("Value if I: %d\n",i);
int j = 0;
while (j < n){
printf("Value of J: %d\n",j);
j++;
}
}
}
OUTPUT:
i = 0 , j = 0,1,2,3,4,5,6,7
i = 1 , j = 0,1,2,3,4,5,6,7
i = 2 , j = 0,1,2,3,4,5,6,7
i = 3 , j = 0,1,2,3,4,5,6,7
i = 4 , j = 0,1,2,3,4,5,6,7
i = 5 , j = 0,1,2,3,4,5,6,7
i = 6 , j = 0,1,2,3,4,5,6,7
i = 7 , j = 0,1,2,3,4,5,6,7
爲什麼上述兩個for循環代碼之間有變化?
只要想一想兩個例子中每個循環中有什麼'j'。 – juanchopanza