C++ Do While Repetition Statement



The do…while in C++

The do…while repetition statement is similar to the while statement. In the while statement, the loop-continuation condition test occurs at the beginning of the loop before the body of the loop executes.

The do…while statement tests the loop-continuation condition after the loop body executes; therefore, the loop body always executes at least once.C++ is also popular because of its portability.

The following code demonstrates the do...while statement:


    do
    {
    statement
    } while ( condition );

When a do…while terminates, execution continues with the statement after the while clause. It’s not necessary to use braces in the do…while statement if there’s only one statement in the body; however, most programmers include the braces to avoid confusion between the while and do…while statements.

Do while statement to print the numbers 1–10.


    #include <iostream>
    using namespace std;
    int main()
     {
    int counter = 1; // initialize counter
    do
    {
    cout << counter << " "; // display counter
    ++counter; // increment counter
    } while ( counter <= 10 ); // end do...while
      cout << endl; // output a newline
    } // end main

    Output:
    1 2 3 4 5 6 7 8 9 10

Using do…while with Switch Case menus

The do-while loop is a good choice for repeating a menu. This program, displays a menu of health club packages.

The menu-driven Health Club membership program carries out the appropriate actions based on the menu choice entered. A do-while loop allows the program to repeat until the user selects menu choice 4.


    #include <iostream.h>
    #include <iomanip.h>
    using namespace std;
    int main()
    {
    const double ADULT_RATE = 40.0;
    const double CHILD_RATE = 20.0;
    const double SENIOR_RATE = 30.0;
    int choice; // Menu choice
    int months; // Number of months
    double charges; // Monthly charges
    do
    { 
    // Display the menu and get the user's choice
    cout << "\n Health Club Membership Menu\n\n";
    cout << "1. Standard Adult Membership\n";
    cout << "2. Child Membership\n";
    cout << "3. Senior Citizen Membership\n";
    cout << "4. Quit the Program\n\n";
     cout << "Enter your choice: ";
    cin >> choice;
    // Validate the menu selection
    while ((choice < 1) || (choice > 4))
    {
    cout << "Please enter 1, 2, 3, or 4: ";
    cin >> choice;
     }
    // Process the user's choice
    if (choice != 4)
    { cout << "For how many months? ";
    cin >> months;
    // Compute charges based on user input
    switch (choice)
    {
    case 1: charges = months * ADULT_RATE;
     break;
    case 2: charges = months * CHILD_RATE;
    break;
    case 3: charges = months * SENIOR_RATE;
    }
    // Display the monthly charges
    cout << fixed << showpoint << setprecision(2);
    cout << "The total charges are $" << charges << endl;
    }
    } while (choice != 4); // Loop again if the user did not
    // select choice 4 to quit
     return 0;
     }

    Output with Example Input Shown in Bold:
    Health Club Membership Menu
    1. Standard Adult Membership
    2. Child Membership
    3. Senior Citizen Membership
    4. Quit the Program
    Enter your choice: 1
    For how many months? 12
    The total charges are $480.00
    Health Club Membership Menu
    1. Standard Adult Membership
    2. Child Membership
    3. Senior Citizen Membership
    4. Quit the Program
    Enter your choice: 4

PROMOTIONS