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

后端 未结 8 747
悲&欢浪女
悲&欢浪女 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:19

    The best practice is to use != only with iterators (C++) and < otherwise. Never ever use == or != with floats/doubles. The following loop is an infinite loop:

    for (double i = 0.0; i != 1.0; i += 0.1)
        printf("yes, I'm an infinite loop ;)");
    

提交回复
热议问题