SEARCH THIS BLOG

do while LOOP IN C/C++

What is do while loop in C/C++?

The do while loop in C/C++ is a flow of control construct which allows the programmer to repeat a set of statements any desired number of times. Such programming constructs are really useful as instead of writing the same statement n number of times we can simply write it once and repeat it n times.

Suppose you have to print all numbers from 1 to n, where n is to be entered by the user. This n can be in thousands and even lacs. It would be impossible for a person to write thousand cout statements. In such cases he can simply write one cout statement and ask the compiler to repeat it n number of times. The do while loop is one of the tools of telling the compiler to repeat the statement.

The major difference between do while loop and the other loops is that do while is an exit controlled loop. This means that the condition is checked after the body of loop is executed.

 

Let us learn in details about while loop in C/C++ :

General Syntax :

initialization;

do

{

    // body of the loop

     update;

} while (condition) ;

Note : A semicolon is put after while (condition) part.

Let us read about the working of do while loop.

 

How does do while loop work?

As we have seen in the syntax, there are four segments associated with any loop. They are initialization, the body of the loop, update statement and condition. Let us learn about them :

Initialization : It is the first step in do while loop. This step initializes the loop variable with an initial value. A loop variable is the variable on which the condition is checked before executing the body of the loop.

Body of the loop : It is the set of statements that needs to be repeated.

Update : After the body of the loop is executed, the control moves to the update statement. An update statement changes the value of the loop variable. It is not mandatory to update the loop variable at last. This can be done anywhere inside the loop.

Condition : In do while loop, the condition is checked at last. After update statement, if the condition is true, the control moves to the body again , otherwise it comes out of the loop.

As you can see that the condition is placed after the body of the loop, therefore whatever be the condition, the body is definitely executed at least once. This is strange as other looping constructs such as for and while check the condition prior to execution of the body.

The following flowchart helps you understand clearly the flow of control in a do while loop :

 

do while loop in C/C++


Let us visualize the flowchart with a demo code.

//The loop part to print all

//numbers from 1 to 99

int i = 1;   // initialization

do

{

   cout<< i << “ ”; // body of loop

   i++;  // update

} while (i < 100) ;  // condition

 

The equivalent flowchart :

 

 

do while loop in C/C++

 


A demo program with detailed dry run 

//sum of odd numbers from 1 to n

#include<iostream>

using namespace std;

int main()

{

   int n;

   cin>>n;

   int sum=0;

   int i = 1;     // initialization

   do

   {

       sum=sum+i;  // body

       i = i + 2;         // update

   }    while(i <= n) ; // condition

   cout<< “Sum is ”<<sum;

   return 0;

}

Input : 10

Output : Sum is 25

Let us see how control flows in this program :

1. The program declares an integer variable n and then takes input from  the user.

2. Another variable named sum is created and initialized with 0 (zero). This variable stores the sum of numbers.

3. As the program is to find sum of odd numbers from 1 to n, we initialize the loop variable (i) with 1.

4. The control enters the body of the loop without any condition check.

5. Inside the loop, the value of i is added to sum and the result is stored again into sum.

6. Then the control moves to update statement where value of  i is increased by 2 so that it becomes the next odd number.

7. Control now checks the condition and if found true, all steps from step 5 are again repeated.

8. If condition is false, the control moves out of the loop and prints the sum.


An important note :

The looping part of same program can also be written as :

do

{

   if(i % 2 != 0)

      sum = sum + i;

   i++;

} while(i<n);


What is the difference between the two logics?

Obviously, both the logics produce the same result. But the difference exists in the number of times the loop is repeated. In the first logic the loop is repeated n/2 times while in second logic, the loop is executed n number of times. So, although both give the same output, the time taken to execute the second logic will be almost twice the time taken to execute the first. For small values of n, this doesn’t make any difference as the time taken is just a fraction of a second. But when value of n becomes very large, the slow execution of second logic becomes noticeable. It is always advised that you should write a code that takes minimum time to give the desired output.



Related topics :

for loop in C/C++

while loop in C/C++

 

If you want to add any information or if you find something incorrect in this article, comment down or contact us through the mail : coding.nkcoder@gmail.com.  

Which topics you want an article on? Comment down on this post.

Do not stop here. Read all articles related to C++ here.

If you want to contribute any article for the website, feel free to share it on coding.nkcoder@gmail.com. We will publish it on the website with your name.

Download the pdf form of this article here.

 


Post a Comment