Understanding Do...While Loop in C++

Understanding Do...While Loop in C++

26 Jul 2025
Beginner
3.95K Views
10 min read
Learn with an interactive course and practical hands-on labs

Free C++ Online Course With Certificate

Do...While Loop in C++: An Overview

Now, it's time to look at the third and the last loop in our C++ programming tutorial i.e. thedo...while loop. After understanding the for loop and while loop in detail, it will be effortless for you to understand the do...while loop in every aspect. To enhance your skills further and gain in-depth knowledge, make sure to enroll in our C++ Online Course Free for a complete learning experience.

Do...While Loop in C++

It is an exit-controlled loop. It prints the output at least once before checking the condition. Afterwards, the condition is checked and the execution of the loop begins.

Do...While Loop in C++

Syntax

do{
//code to be executed
}while(testCondition);
  • The body of the loop executes before checking the condition.
  • If the testCondition is true, the loop body executes again.
  • Again the testCondition is evaluated.
  • The process repeats until the testCondition becomes false.

Example to demonstrate do...while loop in C++ Compiler

#include <iostream>
using namespace std;
int main() {
 int i = 0;
 do {
 cout << i + 1 << endl;
 i++;
 } while (i < 10);
 
 return 0;
}
  • The above code prints numbers from 1 to 10 using the do...while loop in C++.
  • It prints 1 before checking if i<10.
  • It checks the condition i<10 and executes until i becomes 10

Output

1
2
3
4
5
6
7
8
9
10

Read More - C++ Interview Interview Questions and Answers

Nested Do...While Loop in C++

It is a do...while loop inside any do...while loop.

Nested Do...While Loop in C++

Example to demonstrate Nested do...while loop in C++

 #include <iostream>
using namespace std;
int main() {
 int i = 1;
 
 do {
 int j = 1;
 do {
 cout << i << " * " << j << " = " << i * j << endl;
 j++;
 } while (j <= 10);
 i++;
 } while (i <= 2);
 
 return 0;
}
  • The above code creates and outputs multiplication tables of 1 and 2 from 1 to 10 using a nested loop structure.
  • It iterates over the values of i (1 and 2) and j (1 to 10), outputting the result of i * j after each iteration using two nested do...while loops.

Output

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20

Infinite do...while loop in C++

It gets created when the testCondition remains true.

Syntax

do 
{ 
 // body of the loop 
}while(true);

Example to demonstrate infinite do...while loop in C++ Editor

 #include <iostream>
using namespace std;
int main() {
 do {
 cout << "This is an infinite do-while loop." << endl;
 } while (true);

 return 0; // This return statement may never be reached.
}

Output

This is an infinite do-while loop.
This is an infinite do-while loop.
This is an infinite do-while loop. 
...
Summary
We have completed all three loops in C++ programming. Loops in C++ have a broad range of applications, from loop-driven algorithms to iterative problem-solving. As demonstrated, the syntax for using these loops is relatively straightforward, although their logic must be carefully explored to determine advantage and ease of use. For more understanding, you can consider our C++ Certification program.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

He is a renowned Speaker, Solution Architect, Mentor, and 10-time Microsoft MVP (2016–2025). With expertise in AI/ML, GenAI, System Design, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development, he bridges traditional frameworks with next-gen innovations.

He has trained 1 Lakh+ professionals across the globe, authored 45+ bestselling eBooks and 1000+ technical articles, and mentored 20+ free courses. As a corporate trainer for leading MNCs like IBM, Cognizant, and Dell, Shailendra continues to deliver world-class learning experiences through technology & AI.
Live Training - Book Free Demo
Azure AI Engineer Certification Training
20 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Azure AI & Gen AI Engineer Certification Training Program
20 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack .NET Developer with Gen AI Certification Training
21 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
ASP.NET Core Certification Training
21 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Azure DevOps Certification Training
24 Sep
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this