Loops are a very important part of any programming language. They provide the facility of iteration in our code. In other words, whenever we wish to execute a group of statements more than one time, loops come in handy.

Both the while and do while loops execute a statement or a group of statements repeatedly if a particular condition is satisfied. So all you need is a condition to be evaluated and, of course, the statements which you want to execute.

Let us now check out  these loops one by one.

WHILE LOOP

DEFINITION

The while loop executes a group of statements again and again till a particular condition evaluates to be true.

SYNTAX

while(condition to be evaluated)
 {
 block of statements;
 }

WORKING

To understand how a while loop works, let us take an example.

Ex: Suppose we wish to print numbers from 1 to 10.

The code to do this would be:

int i=1;  
while(i<=10) 
{ cout<<i<<" ";
  i=i+1; 
}

Let us understand the code written above.

int i=1;

Initialize the variable to be used in the test condition.

while(i<=10)

while loop begins, executes till i less than or equal to 10

{ cout<<i<<” “;
i=i+1;
}

Print the number i.e i and then increase the value of i for the next iteration.

How do we exit the loop?

Once i becomes 10, it is printed and then i is incremented to 11.

So in the next iteration, test condition is violated and the loop ends.

DO-WHILE LOOP

DEFINITION

The do-while loop also executes a group of statements again and again. But it is slightly different from the while loop. And the difference is that in this looping construct, the test condition is placed at the end of the loop.

So that means statements in the do-while loop are executed atleast once. Only after the first round of execution, the test condition is checked. If it is found to be true, loop is executed again else it is not.

SYNTAX

do
 {
 statements;
 }while(test condition);

WORKING

Let us understand the working of the do-while loop with the help of the same example we used for the while loop above.

int i=1; 
 do 
 { cout<<i<<" "; 
  i++;
 }while(i<=10);

Let us see how this works.

int i=1; 

First we initialize our test variable i to 1.

do 
{ cout<<i<<” “; 
i++;

Then the loop starts and prints the value of i after which i is incremented.

}while(i<=10); 

After this, the value of i is checked in the test condition.If the value is less than 10, the loop executes again.

So this is done till i reaches value 10.

Now how do we exit this loop?

Once 10 is printed, i becomes 11 in the next statement. So test condition is violated and we exit from the loop.

Note that there is a semicolon at the end of the test condition in the do-while loop.

Hope you’ve understood the basic functioning of these two looping constructs in C++. If you have any queries, let us know in comments below.



ABOUT THE AUTHOR:

Founder & Senior Editor of ChewCode.com and Editor at GizmoBolt.com. Sakshi is currently pursuing MCA. She is a foodie and has a unique love for learning computer programming languages. Follow her on Google + | Facebook | Twitter. Contact her at [email protected] for feedback and sending tips.


Tags: , , , , , ,

Leave A Response