Month End Sale: Get Extra 10% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Understanding Do...While Loop in C++

Understanding Do...While Loop in C++

21 May 2024
Beginner
1.09K Views
10 min read
Learn via Video Course & by Doing Hands-on Labs

Free C++ Course Online with Certificate [Full Course]

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.

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

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Software Architecture and Design Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
.NET Solution Architect Certification Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
Azure Developer Certification Training Jul 28 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Data Structures and Algorithms Training with C# Jul 28 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Microsoft Azure Cloud Architect Aug 11 SAT, SUN
Filling Fast
03:00PM to 05:00PM (IST)
Get Details
Angular Certification Course Aug 11 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
ASP.NET Core Project Aug 24 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this