While loop

Do while loop in Cpp programming language

Do while loop in Cpp programming language

In this tutorial, we will discuss do while loop in Cpp programming language

 

In the C++ language, there are three types of basic looping statements

for loop

while loop

do while loop

Do while loop in C++

do while loop

In the C++language, do- while loop is used for execution and evaluation of body of code repeatedly until the test expression is false. The do-while loop is executed when the Boolean expression is evaluated until the condition is satisfied. Do while loop is executed at least once before while  part is executed.

Do while loop contains two-part

  • Do part – Do part is a staring point of do while loo. do part is always executed.
  • While part– Do part is a processing point of the do-while loop.

 

Declaration

The syntax of the do-while loop

do{
      //statement(s) to be executed
}while(condition);

flow diagram of while loop in Java

Do- while loop in Java

How do-while loop works:

In the above diagram, initially, execution starts and flow of control enters the body of the do-while loop and statement is executed only once.

Then,  the test expression is evaluated.

When the test expression is true,  this process continues until the test expression becomes false.

When test expression is false, the do-while loop stops the execution and goes to rest.

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i=1;
    do{
        cout<<"i is :"<<i<<endl;
        i++;
    }while(i<=10);
    //when the test expression is true
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

i is :1 
i is :2 
i is :3 
i is :4 
i is :5 
i is :6 
i is :7 
i is :8 
i is :9 
i is :10

In the above program, it displays 1 to 1 positive number using the do-while loop in C++

Find the sum of given numbers using the do-while loop

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int sum=0,i=0;
    int n;
    i++;
    cout<<"Enter number for n :";
    cin>>n;
    do{
        sum+=i;
        i++;
    }while(i<=n);
    cout << "Total is: " << sum<<endl;
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

Enter number for n :10
Total is :55

In the above program, it finds the sum of given positive numbers using the do-while loop in C++

 

Displaying Array element in the used Do-while loop

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    string name[]={"Jhon","Khan","Robert","Seela"};
    //array index starts with zero
    int i=0;
    do{
            cout<<"Student name "<<i+1<<" is "<<name[i]<<endl;
    i++;
    }while(i<=3);
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

Student name 1 is :Jhon
Student name 2 is :Khan
Student name 1 is :Robert
Student name 1 is :Seela

In the above program, it displays single dimension array of elements using the do-while loop in C++

Find factorial of a number in C++ language

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int n,i=1,f=1;
    cout << "Enter the number" << endl;
    cin>>n;
    //Do while loop to calculate factorial
    do
    {
        f=f*i;
        i++;
    }while(i<=n);
    cout << "The factorial of " <<n<<" is "<<f<< endl;
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

Enter the number
5
The factorial of 5 is:120

In the above program, it finds the factorial of given positive numbers using the do-while loop in C++

Suggested for you

Nested While in Java

Nested while in C

Nested while in C++

Nested while in Python

Nested do while in Java

Do while loop in Java programming language
Nested do while loop in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago