When is a do-while appropriate?

后端 未结 13 1784
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条回答
  •  Happy的楠姐
    2020-11-30 09:51

    I usually use a do-while when something needs to happen, but it won't necessarily happen "correctly" on the first time. For instance:

    int x;
    do
    {
        x = random.next(100);
        cout << x << endl;
    } while (x != 13);
    

    In this case, the x you start with doesn't matter at all, because it's overwritten.

提交回复
热议问题