loop's counter i as I++ vs. i+1 as position in an array

前端 未结 7 1412
甜味超标
甜味超标 2021-01-13 23:25

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 a

相关标签:
7条回答
  • 2021-01-13 23:51

    Let's define i=0, then

    • i++ will return 0 and afterwards increment i to 1
      (post increment: returns value, afterwards increase)
    • ++i will increment i to 1 and afterwards return 1
      (pre increment: increases, afterwards return value)
    • ì+1 will not increment i and return 1
      (addition: only return the result, doesn't touch the variable)

    I assume, you don't want to increment i by itself, only add 1 to access an index with a different offset.

    0 讨论(0)
提交回复
热议问题