100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

do-while Loop in C++

Learn how the C++ `do-while` loop guarantees at least one execution by checking its condition after the body runs, with a full iteration trace.

LoopsBeginner6 min readJul 7, 2026
Analogies

1. Intro

The do-while loop is an exit-controlled loop: it runs the loop body first and only afterward checks whether it should repeat. This single distinguishing fact — the condition is evaluated AFTER the body — means a do-while loop always executes its body at least once, no matter what the condition is. This is the most important thing to remember about do-while, and it is a common source of exam trick questions.

🏏

Cricket analogy: A do-while loop is like a bowler who must bowl at least one over before the umpire even checks whether the innings should continue — the ball is bowled first, and only then is the over-limit condition checked, unlike a pre-checked declaration.

2. Syntax

cpp
do {
    // loop body
} while (condition);

3. Explanation

Execution starts with the do block: the body runs unconditionally the first time. Only after the body finishes does C++ evaluate condition. If it is true, control jumps back to the top of the body and runs it again; if false, the loop ends. Note the required semicolon after while (condition); — omitting it is a compile error. Because the check comes last, do-while is ideal for menu-driven programs or input validation where you must show a prompt or process something at least once before deciding whether to repeat.

🏏

Cricket analogy: Execution starts with the umpire signaling play (do block) unconditionally; only after the over finishes does the umpire check the innings condition — true means bowl another over, false ends it, and forgetting the closing semicolon after while() is like an incomplete declaration; this pattern suits toss-and-play formats where you must bowl at least once before deciding to continue.

4. Example

cpp
#include <iostream>
using namespace std;

int main() {
    int i = 1;
    do {
        cout << "i = " << i << endl;
        i++;
    } while (i <= 3);

    // Demonstrates body runs once even if condition starts false
    int n = 10;
    do {
        cout << "n was " << n << ", body still ran once" << endl;
    } while (n < 5);

    return 0;
}

5. Output

text
i = 1
i = 2
i = 3
n was 10, body still ran once

6. Key Takeaways

  • do-while checks its condition AFTER the body, so the body always runs at least once.
  • The while (condition); line must end with a semicolon.
  • Use do-while when the body must run before it makes sense to test the condition, e.g. menus or input prompts.
  • Trace carefully: with int i = 1; do {...i++;} while (i <= 3); the body runs for i = 1, 2, 3 (three times) before stopping.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#DoWhileLoopInC#While#Loop#Syntax#Explanation#Loops#StudyNotes#SkillVeris