In a “for” statement, should I use `!=` or `<`?

后端 未结 8 776
悲&欢浪女
悲&欢浪女 2021-02-02 05:39

I\'ve seen both of these two for statements:

for(i=0;i<10;i++) 

for(i=0;i!=10;i++)

I know they all stop when i reaches 10 , bu

8条回答
  •  暖寄归人
    2021-02-02 06:18

    Both will work in most situations.

    If for some reason the body of code executed in the loop changes i to something greater than 10, the first version will stop looping, but the second will execute forever.

提交回复
热议问题