C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Monday, January 13, 2014

Purpose and Example of do while Loop in CPP Language

What is a do while Loop?

The do while loop is another iterative statement in C++ language. It is used to repeat a set of one or more statements while the given condition remains true. It is similar to while loop in C++ except that the condition is written after the body of loop.
Purpose and Example of do while Loop in CPP Language
Purpose and Example of do while Loop in C++ Language

Syntax of do while loop


do
{
   statement 1;
   statement 2;
   .
   .
   .
   statement N;

while (condition);

Where:
do is a reserved word or keyword.
{
   statement 1;
   statement 2;
   .
   .
   .
   statement N;

is body of loop (a set of one or more statements enclosed in curly brackets)
while is a reserved word or keyword
condition is normally given as a relational expression like (n>=1) etc

Example of do while loop

int n = 1;
do
{
    cout<<n<<endl;
    n++;
 } 
 while (n<=5); 

Output of the above written C++ code segment is list of 1 to 5 numbers as follows:

1
2
3
4
5

How this output comes?

1. First of all an integer variable n is intialized to 1.
2. Body of loop is executed for first time. 1 is printed and control goes to next line due to endl.
3. The valuse of n is incremented by one so that n is equal to 2 now.
4. Condition is checked for first time n<=5, that is, 2<=5 which is true.
5. Body of loop is executed for second time.
6. 2 is printed and control goes to next line due to endl.
7. The value of n is incremented by one so that n is equal to 3 now.
8. Condition is checked  n<=5, that is, 3<=5 which is true.
9. Body of loop is executed for third time.
6. 3 is printed and control goes to next line due to endl.
7. The value of n is incremented by one so that n is equal to 4 now.
8. Condition is checked  n<=5, that is, 4<=5 which is true.
9. Body of loop is executed for fourth time.
10. 4 is printed and control goes to next line due to endl.
11. The value of n is incremented by one so that n is equal to 5 now.
12. Condition is checked  n<=5, that is, 5<=5 which is true.
13. Body of loop is executed for fourth time.
14. 5 is printed and control goes to next line due to endl.
15. The value of n is incremented by one so that n is equal to 6 now.
16. Condition is checked  n<=5, that is, 6<=5 which is false. So the do while loop ends here.

Example 2 of do while loop


int n=100;
do
{
   cout<<n<<endl;
   n++;
}
while (n>=1000);

Output is as follows:


100

Explanation of working of above sample do while code:

1. First of all the value of n is set to 100.
2. Body of loop is executed for the first time.
3. 100 is printed on screen and control goes to next line due to endl. The value of variable n is incremented by one so that now n = 101
4. The given condition (n>=1000) is checked. Since the condition (101>=1000) is false so the loop will will end.
Share:

0 comments:

Post a Comment

We Love To Hear From You!

EasyCPPprogramming.blogspotcom

Labels