[英]The iteration variable is +1 outside the for-loop?
I have an array of size 4 and I want to check if the array contains the number 8 (which it obviously does not, the code is just for testing). 我有一个大小为4的数组,我想检查该数组是否包含数字8(显然它不包含数字8,代码仅用于测试)。
In the for-loop j goes from 0 to 3, so the final value of j in the loop is 3. However I don't follow why the value of j after the loop has been changed to 4, why is it still not 3? 在for循环中,j从0变为3,因此循环中j的最终值为3。但是,我不理解为什么循环后 j的值已更改为4,为什么还不是3 ?
public class Test {
public static void main (String[] args) {
int[] a = new int[4];
a[0] = 2;
a[1] = 3;
a[2] = 4;
a[3] = 5;
int n = a.length; // n = 4
int number = 8;
int j;
for (j = 0; j < n; j++) {
if (a[j] == number) {
System.out.println("The number is at place " + j);
break;
}
// Last value of j is 3:
System.out.println("Value of j after each iteration " + j);
}
// But here j is 4?
System.out.println("Value of j after the for-loop: " + j);
}
}
The output: 输出:
Value of j after each iteration 0
每次迭代后j的值0
Value of j after each iteration 1每次迭代1后j的值
Value of j after each iteration 2每次迭代2后j的值
Value of j after each iteration 3每次迭代后j的值3
Value of j after the for-loop: 4for循环后的j值:4
Yes because at the end of a for loop there is an increment to the variable. 是的,因为在for循环的末尾,变量有一个增量。 A for loop can be rewritten as:
for循环可以重写为:
int j = 0;
while(j < n) {
//code
j++;
}
So on the last iteration j
will be incremented, it will go to the condition, and it will be false so the body of the for loop will not be entered. 因此,在最后一次迭代中,
j
将递增,它将到达条件,并且它将为false,因此不会输入for循环的主体。 In order for the loop to end, j
has to be more than or equal to the condition. 为了使循环结束,
j
必须大于或等于条件。
New to programming? 编程新手?
for(initialization; booleanExpression; updateStatement) {
; // Body
}
The steps is, 步骤是,
So the end value should be 4 所以最终值应该是4
Think about it... 考虑一下...
This is your for loop: 这是您的for循环:
for (j = 0; j < n; j++){
//your code here
}
You start your for loop with j = 0 and every time you iterate through it, you have to check if the value is less than n ( j < n ). 您以j = 0开始for循环,每次循环访问时,都必须检查该值是否小于n(j <n)。 To achieve that you have to increment it on every iteration.
为此,您必须在每次迭代中将其递增。
So this is what happens for n = 4: 所以这就是n = 4的情况:
1st iteration: 第一次迭代:
j = 0; 0 < 4 == true; // you execute your code j++; //As you can see you increment before you continue to the next iteration
2nd iteration: 第二次迭代:
j = 1; // j now equals 1 because you incremented it on the previous iteration 1 < 4 == true; // you execute your code j++;
3rd iteration: 第三次迭代:
j = 2; 2 < 4 == true; // you execute your code j++;
4th iteration: 第4次迭代:
j = 3; 3 < 4 == true; // you execute your code j++;
5th iteration: 第5次迭代:
j = 4; 4 < 4 == false; // loop ends
As you can see, when your code is about to start the fifth iteration, the j variable now equals 4 so it doesn't pass the j < n criteria. 如您所见,当您的代码即将开始第五次迭代时,j变量现在等于4,因此它不通过j <n标准。
However, it still incremented in the 4th iteration so you get j = 4. 但是,它仍然在第4次迭代中递增,因此您得到j = 4。
This was how my teacher explained it to me when I was just starting my coding education, I hope it helps you as it helped me! 这就是我刚开始编码培训时老师向我解释的方式,希望它对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.