Does C have a “foreach” loop construct?

前端 未结 12 2220
情书的邮戳
情书的邮戳 2020-12-02 03:49

Almost all languages have a foreach loop or something similar. Does C have one? Can you post some example code?

12条回答
  •  Happy的楠姐
    2020-12-02 04:31

    Eric's answer doesn't work when you're using "break" or "continue".

    This can be fixed by rewriting the first line:

    Original line (reformatted):

    for (unsigned i = 0, __a = 1; i < B.size(); i++, __a = 1)
    

    Fixed:

    for (unsigned i = 0, __a = 1; __a && i < B.size(); i++, __a = 1)
    

    If you compare it to Johannes' loop, you'll see that he's actually doing the same, just a bit more complicated and uglier.

提交回复
热议问题