1. Concept and Definition

A do-while loop is a control flow statement that executes a block of code at least once and then repeats the execution as long as a specified condition is true.

  • Unlike the while loop, it guarantees that the code inside the loop will run at least once, even if the condition is initially false.

  • It is generally used in menu-driven programs, input validation, and situations where the block must execute first.

General Syntax:

 
do {
// statements

}
while(condition);

Components:

  1. do → Keyword that marks the beginning of the loop block.

  2. { } → Curly braces containing one or more statements.

  3. while(condition); → Checks the condition after execution. Must end with a semicolon.

2. Execution Flow – Step by Step

  1. Enter the loop at the do keyword.

  2. Execute all statements inside the { } block.

  3. Check the condition in while(condition).

  4. Repeat the block if the condition is true.

  5. Exit the loop if the condition is false.

Flow Diagram (Text-Based)

 
        Start
          |
          v
Execute statements inside do {}
         |
         v
  Check  condition (while)
      |           \
   True          False
   |              \ 
    v               v
Repeat loop       Exit

3. Basic Example

 
#include <iostream>

using namespace std;
int main() {
int i = 1;
do {
cout << i << " ";
i++;
}
while(i <= 5);
return 0;
}

Output:

 
1 2 3 4 5

Explanation:

  • Start with i = 1.

  • Execute cout << i → prints 1.

  • Increment ii = 2.

  • Check i <= 5 → true, repeat.

  • Continue until i = 6.

  • i <= 5 → false, exit loop.

Observation: Loop executes 5 times.

4. Do-While with Initial False Condition

 
#include <iostream>
using namespace std;
int main() {
int i = 10;
do {
cout << i << " ";
i++;
}
while(i <= 5);
return 0;
}

Output:

 
10

Explanation:

  • Condition i <= 5 is false from the start.

  • Do block executes once anyway, prints 10.

  • Condition checked → false → loop exits.

Key Point: This behavior distinguishes do-while from while loop.

5. Practical Applications of Do-While

  1. Menu-Driven Programs:

 
char choice;
do {
cout << "Enter Y to continue: ";
cin >> choice;
}
while(choice == 'Y' || choice == 'y');
  • Code executes at least once, showing the menu.

  • User decides whether to continue.

  1. Input Validation:

 
int number;
do {
cout << "Enter a positive number: ";
cin >> number;
}
while(number <= 0);
  • Ensures user input is valid.

  • Loop repeats until a positive number is entered.

  1. Repeated Calculations:

 
int sum, n;
char repeat;
do {
cout << "Enter number: ";
cin >> n;
sum += n;
cout << "Do you want to add another number? (Y/N): ";
cin >> repeat;
}
while(repeat == 'Y' || repeat == 'y');
cout << "Total sum: " << sum;
  • Allows user to perform repeated calculations.

  • Loop continues based on user choice.

6. Nested Do-While Loops

Do-while loops can be nested inside each other, like other loops.

Example:

 
#include <iostream>
using namespace std;
int main() {
int i = 1, j;
do {
j = 1;
do {
cout << "(" << i << "," << j << ") ";
j++;
}
while(j <= 3);
cout << endl;
i++;
}
while(i <= 2);
return 0;
}

Output:

 
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)

Explanation:

  • Outer loop runs i = 1 to 2.

  • Inner loop runs j = 1 to 3 for each i.

  • Nested loops help perform multiple repetitive tasks in combination.

7. Common Mistakes in Do-While Loops

  1. Forgetting the semicolon at the end:

 
do {
cout << "Hello";
}
while(true) // ❌ Missing semicolon
  1. Infinite loops:

 
int i = 1;
do {
cout << i << " ";
// i not incremented
}
while(i <= 5); // ❌ i never changes → infinite loop
  1. Wrong condition logic:

 
int num;
do {
cout << "Enter a number > 10: ";
cin >> num;
}
while(num < 10); // ❌ Confusing for beginners
  • Always double-check the condition logic.

\8. Difference Between While and Do-While

FeatureWhile LoopDo-While Loop
ExecutionChecks condition firstExecutes code first, then checks condition
Minimum ExecutionMay run 0 timesRuns at least once
Condition CheckBefore loop bodyAfter loop body
Use CaseWhen execution depends entirely on conditionWhen you want code to run at least once
Syntax EndingNo semicolon after closing braceMust end with semicolon after while(condition);

Example Comparison:

  • While Loop (Condition False Initially)

 
int i = 10;
while(i <= 5) {
cout << i << " ";
i++;
}

Output: (nothing)

  • Do-While Loop (Condition False Initially)

 
int i = 10;
do {
cout << i << " ";
i++;
}
while(i <= 5);

Output: 10

Observation: Do-while guarantees one execution, while while may not execute at all.

9. Real-Life Analogy

  • While loop: “I will start studying only if it is raining.” → If not raining → no study at all.

  • Do-while loop: “I will open the door and then check if it is raining.” → Door opens at least once, then decide.

  • Think of do-while as “execute first, check later”.

10. Step-by-Step Execution Table Example

Code:

 
int i = 1;
do {
cout << i << " ";
i++;
}
while(i <= 3);
Stepi valuePrint?Condition i <= 3Next i
1111 <= 3 → truei=2
2222 <= 3 → truei=3
3333 <= 3 → truei=4
444 <= 3 → falseexit