Skip to content
C++ Better Explained
Go back
C++ do-while Loop: How It Works and When to Use It
Edit page

C++ do-while Loop: How It Works and When to Use It

The do-while loop is the third kind of loop in C++ (after for and while). It has one key difference from the others: it always runs at least once, because it checks the condition after executing the body.


Syntax

do {
    // body — runs at least once
} while (condition);
//         ^--- note the semicolon — required!

Don’t forget the semicolon after the closing while (condition). Forgetting it is the #1 syntax mistake with do-while.


How It Works

int i = 0;
do {
    cout << i << " ";
    i++;
} while (i < 5);
// Output: 0 1 2 3 4

Execution order:

  1. Run the body: print 0, increment to 1
  2. Check condition: 1 < 5 → true → repeat
  3. Run body: print 1, increment to 2
  4. Check condition: 2 < 5 → true → repeat
  5. Run body: print 4, increment to 5
  6. Check condition: 5 < 5 → false → stop

The body ran 5 times.


The Key Difference: Runs at Least Once

With a while loop, if the condition is false from the start, the body never runs:

int x = 10;

while (x < 5) {
    cout << "This never prints" << endl;
}

do {
    cout << "This prints once" << endl;
} while (x < 5);

The while loop body never executes. The do-while body executes once, then checks the condition (which is false), so it stops — but it did run once.


The Classic Use Case: Input Validation

The most common reason to reach for do-while is asking for user input. You always need to prompt at least once, then repeat if the input is invalid:

#include <iostream>
using namespace std;

int main() {
    int age;

    do {
        cout << "Enter your age (must be positive): ";
        cin >> age;

        if (age <= 0) {
            cout << "Invalid age. Try again." << endl;
        }
    } while (age <= 0);

    cout << "Your age is: " << age << endl;
    return 0;
}

Sample session:

Enter your age (must be positive): -5
Invalid age. Try again.
Enter your age (must be positive): 0
Invalid age. Try again.
Enter your age (must be positive): 25
Your age is: 25

Without do-while, you’d need to write the prompt twice — once before the loop and once inside it. do-while eliminates that duplication.


Another great fit: programs with menus where you always show the menu at least once, then repeat until the user chooses to quit:

#include <iostream>
using namespace std;

int main() {
    int choice;

    do {
        cout << "\n--- Menu ---\n";
        cout << "1. Say hello\n";
        cout << "2. Say goodbye\n";
        cout << "3. Quit\n";
        cout << "Choice: ";
        cin >> choice;

        switch (choice) {
            case 1: cout << "Hello!\n"; break;
            case 2: cout << "Goodbye!\n"; break;
            case 3: cout << "Exiting...\n"; break;
            default: cout << "Invalid choice.\n";
        }
    } while (choice != 3);

    return 0;
}

The menu always displays at least once. If the user picks 3, the loop exits. Otherwise, it loops back to show the menu again.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Comparing while vs do-while

// while loop: checks first
int n = 0;
while (n > 0) {
    cout << n;
    n--;
}
// Nothing prints — condition was false from the start

// do-while: runs first, then checks
do {
    cout << n;
    n--;
} while (n > 0);
// Prints: 0 (ran once before checking)

Rule of thumb:


Nested do-while

You can nest do-while inside other loops, or vice versa:

int i = 1;
do {
    int j = 1;
    do {
        cout << i * j << "\t";
        j++;
    } while (j <= 5);
    cout << "\n";
    i++;
} while (i <= 3);

Output (multiplication table):

1  2  3  4  5
2  4  6  8  10
3  6  9  12 15

Common Mistakes

Forgetting the semicolon:

do {
    cout << "hi\n";
} while (true)   // Error: missing semicolon

Infinite loop:

do {
    cout << "infinite\n";
} while (true);  // Runs forever — add a break or condition change

Counter not updating:

int i = 0;
do {
    cout << i;
    // Forgot i++; — infinite loop!
} while (i < 5);

Summary

The do-while loop is simple but has one important property: the body always executes at least once. Use it when you need this guarantee — especially for input validation and menu loops. For everything else, for and while are more common choices.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
C++ Constructors and Destructors Explained
Next Post
C++ Grade Calculator: Build One Step by Step