Hello Readers,
Today, we’ll going to study the do-while loop in C++. In the previous tutorial, we studied the for while loop in C++ and also we figured the difference b/w the for loop and while loop. Here’s the link to that tutorial.
Here’s the Microsoft documentation, if you want to go-through it. We’ve tried to make this tutorial simplest as possible. So, without wasting time, let’s get started.
Till now, in all the loops whether it be while loop or for loop, before entering the loop, we first check the condition. But, in do-while loop, we first execute code inside the loop and then check the condition.
If the above statements, makes you confusing, don’t worry, things will get easier as we move ahead with this tutorial.
Syntax of do-while loop
do{
// code inside the do-while loop
}
while(condition);
Here the syntax is very much changed as compared to the while loop. The program counter executes the code inside the do block & after that, it checks the condition inside the while loop. If the condition inside the while turns to be true, then the code inside the do block is executed & the process repeats.
If you read the above paragraph carefully, then you’ll realise that in do-while loop, the code inside the loop executes 1 time extra then while loop or for loop.
Related: How to start learning programming
Control structure of do-while loop
Below is the control flow of the do-while loop. This is how the do-while loop works internally.
First, the control flow encounters the do block and executes the code inside this block. After the do block, the condition is checked and based on that condition, it makes a decision. If the condition turns to be true, then the program control goes back to the do block and repeats the process. And if the condition inside while, turns to be false, then the program control executes the next line of code and moves on.
If you see the image carefully, you’ll understand the control flow of the do-while loop. The above image is self-explanatory.
Related: Best IDE for C++ programming
Example of do-while loop
Now, we’ve covered enough theory. We have covered all important concepts related to the do-while loop.
So, let’s wrap-up this tutorial with an example to understand the concept better.
Example:
#include <iostream>
using namespace std;
int main(){
// Variable Declaration & Definition
int i=1;
do{
cout<<i<<endl;
i++;
}
while(i>=1 && i<=5);
return 0;
}
Output
1
2
3
4
5
Now, you can compare the condition versus the output that we’re getting. In condition, we want values of i greater than or equal to 1 but less than or equal to 10. But, actually, we are getting extra value of 0. This is what a do-while loop do. In do-while loop, the code inside the loop executes 1 time extra.
So, this was all about the do-while loop in C++. If you have any doubts regarding that, you can post in the comments section, you can also mail us at cpptopics@techoutflow.com.
Since, we’ve covered the loops, we highly recommend you to practice Star Pattern problems. You can practice on any IDE you want, even on online IDE, here’s the link.
The next tutorial will be on continue statement in C++. Till then stay tuned.