Do-While Loop in C++
A do-while loop is used to repeatedly execute a block of code while a given condition is true. It is also called an exit-controlled loop because the condition is checked after executing the loop body.
This means the loop body executes at least one time even if the condition is false.
Syntax of Do-While Loop:
do
{
// code to be executed
} while(condition);
How Do-While Loop Works?
Algo of do-while loop
- First, the loop body executes.
- After execution, the condition is checked.
- If the condition is true, the loop runs again.
- The loop variable is updated inside the loop.
- This process continues until the condition becomes false.
- When the condition becomes false, the loop stops.
Note: In a do-while loop, the loop body executes at least one time because the condition is checked after execution.
Program 1: Print Numbers from 1 to 5
#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:
- The variable i is initialized with value 1.
- The do block executes first without checking the condition.
- Step 1: i = 1 → prints 1
- After printing, i++ increases the value of i.
- Step 2: i = 2 → prints 2
- Step 3: i = 3 → prints 3
- Step 4: i = 4 → prints 4
- Step 5: i = 5 → prints 5
- After that, i becomes 6.
- Now the condition (i <= 5) becomes false.
- Therefore, the loop stops.
Program 2: Table of a Number
#include <iostream>
using namespace std;
int main() {
int num = 7;
int i = 1;
do {
cout << num << " x " << i << " = " << num * i << endl;
i++;
} while(i <= 10);
return 0;
}
Output:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
Explanation:
- The variable num stores the table number 7.
- The variable i is initialized with value 1.
- The do block executes first.
- In each iteration, the multiplication table is printed using: num * i.
- Step 1: 7 × 1 = 7
- Step 2: 7 × 2 = 14
- Step 3: 7 × 3 = 21
- This process continues until i becomes 10.
- After every iteration, i++ increases the value of i.
- When i becomes 11, the condition (i <= 10) becomes false.
- Therefore, the loop stops.
Important Example: Condition is False but Loop Executes Once
#include <iostream>
using namespace std;
int main() {
int i = 10;
do {
cout << "Hello Student";
} while(i < 5);
return 0;
}
Output:
Hello Student
Explanation:
- The variable i is initialized with value 10.
- The condition is (i < 5).
- This condition is already false because 10 is not less than 5.
- But in a do-while loop, the loop body executes first.
- Therefore, the statement "Hello Student" is printed one time.
- After execution, the condition is checked.
- Since the condition is false, the loop stops immediately.
- This example proves that a do-while loop always executes at least one time.
Summary:
- Do-while loop executes the code first and checks the condition later.
- The loop body executes at least one time.
- It is called an exit-controlled loop.
- Always update the loop variable to avoid infinite loop.