Loop condition: why “not-equal” instead of “lower-than”

后端 未结 5 1936
攒了一身酷
攒了一身酷 2021-01-04 08:56

I was assigned to work on an Android-Java (real-time game) project with a considerable (partially legacy) code base.

Most of the loops I see are like this (where mjk

5条回答
  •  被撕碎了的回忆
    2021-01-04 09:35

    I believe that this was just an inexperienced programmer. In general, it's of course always better to use a < for more robustness. In the case of fooling around with the index (e.g. by changing the step interval to i+=2), it will not produce an infinite loop.

    Technically, comparison might use less CPU time (not so familiar with this though), but the effect is marginal or just irrelevant, it will not destroy any program's performance... :)

提交回复
热议问题