[英]Basic Incrementation
i know this is a very basic question BUT. 我知道这是一个非常基本的问题。
I understand the concept behind. 我了解背后的概念。 n++, ++n, n--, --n.
n ++,++ n,n-,-n。 HOWEVER
然而
public static void main(String[] args){
int count = 1;
for(int i =1;i<=10;++i){
count = count * i;
System.out.print(count);
}
}
So it will print: 1 2 3 4 5 6 7 8 9 10. 因此它将打印:1 2 3 4 5 6 7 8 9 10。
My Question is. 我的问题是。 Why if i is incremented as ++i isnt i then treated as 2, instead of 1. Inst the point of ++i, to increment i before it's manipulated by another operation?
为什么如果将i递增为++ i而不是i,然后将其视为2,而不是1。请说明++ i,以便在i被其他操作操纵之前递增i?
Is the point of ++i, to increment i before it's manipulated by another operation?
是++ i的要点,是在i被其他操作操纵之前将其递增?
The difference between ++i
and i++
only matters when it's used as part of a bigger expression, eg ++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
In this case the operation occurs at the end of each iteration of the loop, on its own . 在这种情况下,操作会在循环的每次迭代结束时自行发生 。 So your loop is equivalent to:
因此,您的循环等效于:
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;
}
}
Now changing ++i
to i++
at the end there isn't going to make a difference at all - the value of the expression isn't used for anything. 现在,将
++i
最终更改为i++
根本没有什么不同-表达式的值不用于任何东西。
The increment isn't called until after the first iteration of the for
loop. 在
for
循环的第一次迭代之后才调用增量。
While it's true that 确实是这样
j = i++;
k = ++i;
return different results, think of the ++i
in this context as a standalone line called at the end of every for
loop. 返回不同的结果,在这种情况下,将
++i
视为在每个for
循环末尾调用的独立行。
You want to use i++ which is a post increment. 您要使用i ++,这是后期增量。 ++i is called a preincrement and the difference is precisely as you have pointed out.
++ i被称为预增量,而差异恰恰是您所指出的。
In this case ++i
happens at the end of the loop, it increments and then checks if the new value still meets the termination condition. 在这种情况下,
++i
发生在循环的末尾,它递增,然后检查新值是否仍满足终止条件。
Also, won't the output be: 另外,输出将不会是:
count i
1 * 1 = 1
1 * 2 = 2
2 * 3 = 6
6 * 4 = 24
etc. 等等
The for loop you wrote is same as: 您编写的for循环与:
i = 1;
while(i<=10) {
count = count * i;
System.out.print(count);
i = i + 1;
}
So that's why! 所以这就是为什么!
For i = 0 and While i < 1= 10, print i, and then pre-increment i. 对于i = 0且当i <1 = 10时,打印i,然后预先递增i。 (++i/i++ doesn't make a difference here).
(++ i / i ++在这里没有区别)。
Here try this though: 这里尝试一下:
int i=1; while(i <= 10) System.out.print(++i); i = 1; while (i <= 10) System.out.print(i++);
As an addition to the other answers, the historical reason for preferring for(int i=1;i<=10;++i)
over for(int i=1;i<=10;i++)
is that ++i
does not need to store the old value of i
in an extra variable. 作为其他答案的补充,比
for(int i=1;i<=10;i++)
更喜欢for(int i=1;i<=10;++i)
的历史原因是++i
确实不需要将i
的旧值存储在额外的变量中。 Thus, ++i
is faster than i++
, though the speed improvement is negligible. 因此,
++i
比i++
快,尽管速度的提高可以忽略不计。 On modern compilers this speed improvement is done as an optimization, so the two pieces yield the same compiler output. 在现代编译器上,这种速度改进是作为优化来完成的,因此这两部分产生的编译器输出相同。 However, since
++i
is always as fast or faster (eg, on old C++ compilers) than i++
, many experienced programs always use ++i
within loops. 但是,由于
++i
总是比i++
快或快(例如,在旧的C ++编译器上),因此许多有经验的程序总是在循环内使用++i
。
As other answers have stated, both pieces of code are functionally equivalent (in the case of a for loop). 正如其他答案所述,这两段代码在功能上是等效的(在for循环的情况下)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.