AppDividend
Latest Code Tutorials

Do while Loop in C++ Example | C++ Do-while Loop Program

0

Do while Loop in C++ Example | C++ Do-while Loop Program is today’s topic. Before understanding do while loop, we must have an idea of what loops are and what it is used for. Loops are used when we want a particular piece of code to run multiple times. We use loops to execute the statement of codes repeatedly until a specific condition is satisfied. It eases the work of the programmer and also shortens the code length.

For example, if we want to print a table of particular numbers then with without loops, we had to do calculations multiple times and write different print statements, but with the help of loop, we can write 2-3  lines of code specifying the conditions for the table to print with different iterative variables. It eases the human effort to a very high extent.

Content Overview

Do while Loop in C++

Do while loops are used to iterate over a block of code multiple times. If we are not sure about the number of iterations, then it is of best practice to use the do-while loop.

There are three types of loops for loop, while loop and do-while loop. In this tutorial, we are going to learn about do-while loop.

Note: Do while loop is executed once always because in a do-while loop condition is checked after the loop body.

Syntax of the do-while loop

do
{
  // statements
} while(condition);

Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested.

If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop execute again. This process repeats until the given condition becomes false.

See the formal example.

do
{
  cout << "Hello I am a variable and my value is:" << i << endl;
  i = i - 1;
} while (i > 0);

See the output.

Hello I am a variable and my value is:5
Hello I am a variable and my value is:4
Hello I am a variable and my value is:3
Hello I am a variable and my value is:2
Hello I am a variable and my value is:1

Here the first line gets printed without checking the condition. It only happens in the do-while loop.

Flow chart of a do-while loop

 

Do while Loop in C++ Example

Example program on do-while loops

Q1- Write a program to show the working of the do-while loop.

#include <iostream>
using namespace std;

int main()
{
  int i = 10;
  do
  {
    cout << "i= " << i << endl;
    i = i - 1;
  } while (i > 0);
}

See the output.

 

do-while loop

Q2- Print the table in descending order of 10 using the do-while loop.

#include <iostream>
using namespace std;

int main()
{
  int i = 10;
  do
  {
    cout << "10 x " << i << " = " << 10 * i << endl;
    i = i - 1;
  } while (i > 0);
}

See the output.

 

C++ Do-while Loop Program

C++ Nested do-while Loop

If you use a do-while loop inside another do-while loop, it is known as nested do-while loop.

The nested do-while loop is executed fully for each outer do-while loop.

Let’s see a simple example of a nested do-while loop in C++.

#include <iostream>
using namespace std;

int main()
{
  int i = 1;
  do
  {
    int j = 1;
    do
    {
      cout << i << "\n";
      j++;
    } while (j <= 3);
    i++;
  } while (i <= 3);
}

See the output.

1 1
1 2
1 3
2 1
2 2 
2 3
3 1
3 2
3 3

C++ Infinitive do-while Loop

In C++, if you pass true in the do-while loop, it will be an infinitive do-while loop.

do
{    
  //code to be executed    
} while(true);  

See the following example.

#include <iostream>
using namespace std;

int main()
{
  do
  {
    cout << "Infinitive do-while Loop";
  } while (true);
}

See the output.

Infinitive do-while Loop 
Infinitive do-while Loop
Infinitive do-while Loop
ctrl+c

Finally, Do while Loop in C++ Example | C++ Do-while Loop Program is over.

Recommended Posts

Data Types in C++ Example

C++ Namespace Example

Break Statement in C++ Example

C++ Switch Example

C++ While Loop Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.