When is a do-while appropriate?

后端 未结 13 1799
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:50

    Normally when you need the user to input some value, the counter variable is based on the input value, you use do-while. //The following code is in C++. It requires the user to input a number and repeats until users input number larger than 0.

    do{
        cout << "Enter a Positive integer: ";
        cin >> num;
    }while( num < 0 );
    

提交回复
热议问题