For loop execution, increment confusion

前端 未结 3 1484
既然无缘
既然无缘 2020-12-22 05:10

I don\'t get it why i in fillArray method ends up being equal to 10 even though the array score is filled only up to index 9

3条回答
  •  梦毁少年i
    2020-12-22 06:00

    in For loop, increment occurs after testing loop condition, not before. So in last iteration when your condition is checked, I is already equal to 10 and that is exactly what is being returned. Consider this, if your I would still be 9 in last iteration, your condition would still be true which would mean one more execution in loop.

提交回复
热议问题