use*_*012 1 java loops for-loop increment
我知道这是一个非常基本的问题但是.
我理解背后的概念.n ++,++ n,n - , - n.然而
public static void main(String[] args){
int count = 1;
for(int i =1;i<=10;++i){
count = count * i;
System.out.print(count);
}
}
Run Code Online (Sandbox Code Playgroud)
因此它将打印:1 2 3 4 5 6 7 8 9 10.
我的问题是.为什么如果i增加为++我不是我然后被视为2而不是1.设为++ i的点,在被另一个操作操纵之前增加i?
Jon*_*eet 11
++ i的观点是,在被另一个操作操纵之前增加i吗?
之间的差++i和i++只事项时,它的用作为一个更大的表达式的一部分,例如
int j = ++i; // Increment then use the new value for the assignment
int k = i++; // Increment, but use the old value for the assignment
Run Code Online (Sandbox Code Playgroud)
在这种情况下的操作发生在循环的每次迭代结束时,在其自己的.所以你的循环相当于:
int count = 1;
// Introduce a new scope for i, just like the for loop does
{
// Declaration and initialization
int i = 1;
// Condition
while (i <= 10) {
count = count * i;
System.out.print(count);
// Now comes the final expression in the for loop "header"
++i;
}
}
Run Code Online (Sandbox Code Playgroud)
现在改变++i到i++在到底有没有打算有所作为的话-表达式的值不用于任何东西.