循环计数器i作为I ++与i + 1作为数组中的位置 [英] loop's counter i as I++ vs. i+1 as position in an array
问题描述
我用i做了一个循环,因为它是计数器变量。
在循环中我正在比较一个数组的单元格。
我想知道它们之间的区别是什么array [i ++](或array [++ i])到array [i + 1]。
因为i +(或++ i)不能按照想要的方式工作而i + 1(一个让我疯狂的小包。)
I've made a loop with i as it's counter variable.
Inside that loop I'm comparing cells of an array.
I'm wondering what's the difference between array[i++] (or array[++i]) to array[i+1].
Since i++ (or ++i) don't work in the wanted way while i+1 does (a little bag which drove me crazy).
提前致谢。
推荐答案
-
array [i ++]
将评估array [i]
并增加我
。 -
array [++ i]
将增加我
和然后评估array [i]
(所以它给你下一个元素) -
array [i + 1]
将为您提供下一个元素,而不会增加i
array[i++]
will evaluatearray[i]
and incrementi
.array[++i]
will incrementi
and then evaluatearray[i]
(so it gives you the next element)array[i+1]
will give you the next element without incrementingi
我个人试图避免使用这样的副作用 - 这意味着当我稍后阅读代码时,我总是要放慢脚步以确保我按正确的顺序得到了所有内容。
Personally I try to avoid using side-effects like this - it means when I read the code later, I always have to slow down to make sure I get everything in the right order.
如果你'已经在一个递增 i
的循环中,然后在数组访问表达式中递增 i
意味着每次循环迭代都会增加 i
两次。
If you're already in a loop which is incrementing i
, then incrementing i
in the array access expression as well would mean that each loop iteration would increment i
twice.
这篇关于循环计数器i作为I ++与i + 1作为数组中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!