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++

Similar post

Hello world in Java

Hello world in C++

Hello world in Python

Hello world in C#

 

Suggested for you

for loop in Java

while loop in Java

for loop in C language

while loop in C language

while loop in cpp language

For loop in Cpp language

Nested for loop in C++ language

Nested for loop in Java language

Nested for  loop in C language

Nested for loop in Python language

 

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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago