Updated March 23, 2023
Introduction to Do While Loop in C++
Do while loop is a control statement that controls the flow of the program. Unlike for loop and while loop that checks the condition at the top of the loop, do-while loops check the condition at the bottom of the loop. Do while loop is similar to the while loop; the only difference is while loop first checks the condition and then execute the loop, where do while first execute the loop and then check that condition. That means that in the do-while loop, the loop will execute at least one time. In this article, we are going to see the working of the do-while loop in C++ with the help of examples. The syntax of the do-while loop in C++ programming is as follows.
Syntax:
do
{
statement 1;
statement 2;
statemen n;
}
while(condition);
Here, the keyword is outside the loop, and the statement that needs to be executed is written inside the loop. The while keyword is used outside a loop that ends with a semicolon. while the loop contains the condition part that checks the condition.
Flow Chart of Do While Loop in C++
Below is the flow diagram for the do-while loop in C++, which elaborates the do-while loop’s stepwise execution with the diagram.
Examples of Do While Loop in C++
Below are some of the examples of do while loop in C++:
Example #1
Program to print the number from 0 to 10 in C++.
Code:
#include <iostream>
using namespace std;
int main()
{
int x = 0;
do
{
cout << "Entered number is: " << x << endl;
x++;
}while(x <= 10);
return 0;
}
Output:
Code Explanation: Here, we have written a program to print numbers from 1 to 10 using do while loop in C++ programming. First, we have initialized the variable x to 0. the do loop executes the statement mentioned inside the loop. First, it prints the value of x, and then it increments the value of x by 1 outside the loop while checking the condition to check that whether the value of x is less or equal to 10. If the condition is true, then continue the iteration process; if the condition is false, then stops the execution.
Example #2
Program to print the multiplication table in C++.
Code:
#include <iostream>
using namespace std;
int main()
{
int n, count = 0, limit;
cout << "Enter the value to find the multiplication table: ";
cin >> n;
cout << "Enter the maximum limit for multiplication table: ";
cin >> limit;
do
{
cout << n << "*" << count << " = " << n*count <<endl;
count++;
}
while(count <= limit);
return 0;
}
Output:
Code Explanation: Here, we have written a code to print the multiplication table which the user wants to print. Here we have initialized three variables n to take the number from the user and count to count the number and limit to restrict the limit for the multiplication table. Do keyword will execute the statement. First, it calculates the multiple of the values entered by the user and it prints. The count is incremented by 1 per iteration. While checking the condition, whether the count is greater than or equal to the limit or not. Based on the result, it prints the multiplication table.
Example #3
Program to print elements of an array using do while loop.
Code:
#include <iostream>
using namespace std;
int main()
{
int i = 0;
int array[] = {2,7,19,5,8};
do
{
cout << array[i] << endl;
i++;
}while(i <= 4);
return 0;
}
Output:
Code Explanation: Here, we have written a program to print the array elements using a do while loop in C++ programming. First, we have initialized variable I to 0 and declare the array elements. do loop will print the array elements from the list. i is used as a counter to increment the value by 1. While keyword contains the condition which counts, i.e. i must be less than or equal to 4.
Example #4
Program to add numbers until the user enters 0.
Code:
#include <iostream>
using namespace std;
int main()
{
float f_num, Total = 0.0;
do
{
cout << "Enter a number: ";
cin >> f_num;
Total += f_num;
}
while(f_num != 0.0);
cout << "Toatal Sum = " << Total;
return 0;
}
Output:
Code Explanation: Here, we have written a program to calculate the total of the entered numbers. For a change, here we have applied a condition that states that it will ask a user to enter a number unit it enters 0, and in the end, it calculated the total of the numbers. Note that, here, we have used a float data type. It enables the user to enter the decimal values.
Recommended Articles
This is a guide to Do While Loop in C++. Here we discuss the syntax, flowchart, and various Do While Loop in C++ and code implementation. You can also go through our other related articles to learn more –