For Loop or While Loop - Efficiency

前端 未结 8 1881
遥遥无期
遥遥无期 2021-01-12 08:03

This may be a stupid question, but how does the efficiency of a while loop compare to that of a for loop? I\'ve always been taught that if you can use a for loop,

8条回答
  •  有刺的猬
    2021-01-12 08:35

    Think of the main difference as style: with a for loop, you don't have to go looking for the initial value, threshold, and increment.

    It's also a lot easier to make an infinite loop out of a while because you forgot to increment.

提交回复
热议问题