Loops in C - for() or while() - which is BEST?

前端 未结 4 928
梦谈多话
梦谈多话 2021-01-12 19:28

for() or while() - which is BEST?

for (i=1; i

OR

i=1;
while (i

        
4条回答
  •  自闭症患者
    2021-01-12 20:30

    Which one makes most sense in the situation.

    The for loop tells you it is most probably a fixed count loop. Starting at 1 ending before a.

    The while loop doesn't imply any such thing, just that it ends once i >= a (at least from just reading the while (i at the top).

    Of course, this isn't a rule and programmers generally do as they see fit, but it does make it easy to read through code without having to backtrack to comprehend some section.

提交回复
热议问题