The do-while loop

The do-while loop

Its format is:

do statement while (condition);

Its practicality is strictly identical with the while loop, except that condition within the do-while loop is evaluated when the execution of statement rather than before, granting a minimum of one execution of statement even though condition is never concluded. For instance, the subsequent example program prints any number you enter till you enter zero.

// number print

#include <iostream>

using namespace std;

int main ()

{

unsigned long numbers;

do {

cout << “Enter number (0 to end): “;

cin >> numbers;

cout << “You entered: ” << numbers <<std::endl;

} while (numbers != 0);

return 0;

}

Output

Enter number (0 to end): 12345

You entered: 12345

Enter number (0 to end): 160277

You entered: 160277

Enter number (0 to end): 0

You entered: 0

The do-while loop is typically used once the condition that has got to confirm the terminating condition value of the loop is decided among the loop statement itself, like within the previous case, wherever the user input inside the block is what’s used to determine if the loop has got to finish. In reality if you ne’er enter the value zero within the previous example you’ll be prompted for a lot of numbers forever.

PDF is available here The do while loop