When is a do-while appropriate?

后端 未结 13 1812
Happy的楠姐
Happy的楠姐 2020-11-30 09:15

When is a do-while the better choice over other types of loops? What are some common scenarios where its better than others?

I understand the function of a do-while,

13条回答
  •  没有蜡笔的小新
    2020-11-30 09:45

    No-one's yet mentioned its use in C macros...

    #define do_all(x) do{ foo(x); bar(x); baz(x); } while(0)
    

    then in the code you can have

    do_all(a);
    
    • the point being that it gets executed exactly once and the semi-colon on the end of the macro call completes the while(0); statement.

提交回复
热议问题