我不明白这个 for 循环是如何工作的
for (int i = 0; i < 20-i; i+=3) {
System.out.println(--i);
}
为什么初始值是-1以及它如何增加该值?
for (int i = 0; i < 20-i; i+=3) {
System.out.println(--i);
}
Why is the initial value -1 and how is it incrementing that value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
--i
表示在使用之前将i
减去1。如果您输入i--
,则意味着获取i
的值,然后在使用它后减去 1。在每个循环中,您要加 3,但在使用该值之前要减 1。所以换句话说,你实际上只是加了 2。你使用的
i
的值是:-1, 1, 3, 5, 7
这有点令人困惑!编辑:只是添加一下,它确实会在那一点停止,因为下次循环时您将得到
7+3
其中=10
和10 !< 20-10
因此它无法进行该迭代。--i
means subtract 1 fromi
before using it. If you'd puti--
then it'd mean take the value ofi
and then subtract 1 after using it.In each loop you are adding 3, but then you are subtracting 1 before you use the value. So in other words you are in effect only really adding 2. The values of
i
you are using are:-1, 1, 3, 5, 7
which is a bit confusing!EDIT: Just to add, it does stop at that point, because the next time through the loop you'd have
7+3
which=10
and10 !< 20-10
so it can't do that iteration.因为你有 --i,如果你有 i-- 第一个输出将为 0。
--i 将从答案中减去一个,因为 ++i 会在答案中加一。
因此,无论您的循环构造什么输出,由于 --i 的原因,始终会从最终输出中扣除 1。
如果你有 i-- 它仍然会从最终答案中扣除 1,但是直到循环结束才会扣除 1。
Because you have --i, if you had i-- the first output would be 0.
--i will take one off the answer as ++i will add one to the answer.
So, whatever output your loop is constructing, 1 will always be deducted from the final output because of the --i.
If you had i-- it would still deduct one from the final answer, but it would what until the loop had ended to deduct 1.
在 println 中您显示 --i。 -- 减少 i 的值。首先在循环中 i = 0。
当你递减 0 时,你会得到 -1。
for 循环本身在每次迭代时都会添加 i,其中 i+=3。
Inside the println you are displaying --i. -- decrements the value of i. To start with in the loop i = 0.
When you decrement 0 you get -1.
The for loop itself is adding to i with the i+=3 on each iteration.
--i 是预减运算符。首先递减 i 中的值,然后访问它进行打印,因此值为 -1
i-- 是后递减运算符。首先访问 i 中的值,然后递减值,因此值将为 0
--i is pre decrement operator. First decrements the value in i and then access it to print, so value is -1
i-- is post decrement operator. First access value in i and then decrements value, so value will be 0
您的循环变量在 for 语句和主体中都被修改了。首先对表达式
--i
进行求值,得到 -1。然后 for 语句通过添加 3 来修改它。下一次循环时,它应该在打印之前再去掉 1,等等。Your loop variable is bing modified both in the for statement and in the body as well. First the expression
--i
is evaluated, yielding -1. Then the for statement modifies it by adding 3. Next time through the loop it should take away 1 more before printing, etc.--i 首先递减 i 的值,并将 -1 打印为第一个值。在每个周期结束时,i 增加 3。
然后 i 在每个周期中进入
我-> -1-> 2
我-> 1-> 4
直到
我-> 7-> 10
--i first decrements the value of i and the -1 printed as first value. At the end of each cycle i is incremented by 3.
Then i goes in each cycle as
i -> -1 -> 2
i -> 1 -> 4
until
i -> 7 -> 10
在 for 循环中,首先有一个赋值 - 在本例中为
i = 0;
- 这是 i 的起始值。下一部分是条件 - 这里
i
i
i
i
i
i
i
i
i
20 - i
- 在进入循环之前,将在循环的每一步进行检查。第三部分有条件 -
i += 3
这将在每次迭代中将 i 增加 3。所以..
现在您可以从
i = 0
开始,i
i
i
i
i
i
i
i
i
i
i
i
i
i 20 -i
为true
,因此进入循环,并执行语句
System.out.println(--i);
这是预减运算符,因此它将 i 减 1,然后打印它。
只要条件
i
i
i
i
i
i
i
i
20 -i
为true
。In the for loop, first you have an assignment - in this case
i = 0;
- this is the starting value of i.The next part is the condition - here
i < 20 - i
- this will be checked at each step of the loop before entering it.The third part has the condition -
i += 3
This will increment i by 3 in each iteration.So..
Now you have
i = 0
to start with,i < 20 -i
istrue
, so it enters the loop,and it executes the statement
System.out.println(--i);
This is the pre decrement operator, so it decrements i by one, and then prints it.
This is repeated as long as the condition
i < 20 -i
istrue
.--i 是预减运算符。它首先将 i 中的值减 1,然后打印它,
因此,如果 i 的值当前为 0,则第一个该值减 1,这意味着
i==i-1
然后打印它i=-1
--i is pre decrements operator. it first decrease the value in i with 1 and then print it ,
so if the value of i is currently is 0 the 1st this value is reduced by 1 that means
i==i-1
then print iti=-1