Search Placementyogi.com
 

While and Do-While loop in C

There are 2 variations of while loop in C

  1. while loop
  2. do while loop

While Loop in C

 while loop in C

The while loop is the most fundamental loop statement in C and continuously executes a set of statements while the condition is true.

Syntax of while loop in C:

while(condition)
{
    //statements
}

Example of while loop on C:

int i=5;
while(i<10)
{
	printf("i=%d",i);
	i++;
}

Output:

i=5

i=6

i=7

i=8

i=9

Here, 1st the condition(i<15) is checked, if it is true then the statements inside the while loop body are executed,  after the last statement is executed, the control again goes to the condition. The control enters the body of while loop only when the condition is still true.

Note: i++ is very important here, without it the while loop becomes an infinite loop.

 

A Real World Example of while loop in C:

while(are there cookies in the jar?)
{
	eat one cookie;
}
close the cookies jar.

Consider that your Mom has gone out, and asked you not to eat the cookies kept in the kitchen. But they are your favorite cookies and obviously, you are gonna eat them up!

1. You open the cookie jar 
2. Check if there are cookies in it. 
   If no, close the jar and run away!(Condition)
3. Take one cookie and eat it, which decreases 
   the number of cookies in the jar by 1.
4. Check again if there are more cookies in the jar . (i.e. repeat step 2)
5. Take another cookie and eat it which again decreases 
   the number of cookies in the jar by 1.
6. Check again if there are more cookies in the jar . (i.e. repeat step 2)
7. Take another cookie and eat it which again decreases the 
   number of cookies in the jar by 1.

Notice that the steps 4 and 6 are same as step 2, and steps 5 and 7 are same as step 3.

The above example can be re-written as:

1. You open the cookie jar 
2. Check if there are cookies in it. if No go to step5 (Condition)
3. Take one cookie and eat it, which decreases the number of cookies in the jar by 1.
4. Go to step 2
5. Close the jar and run away!

do while loop in C

do while loop is almost same as while loop except one difference:

The do while loop executes the statements in the body of the loop at least once!

In the previous cookies example, Lets say that you already know that there is atleast one cookie in the jar, so you need not check for the first time if there is cookie in the jar, you just know it!

Real World Example of do while loop in C:

1. You open the cookie jar 
2. Take one cookie and eat it, which decreases the number of cookies in the jar by 1.
3. Check if there are more cookies in it. 
   if Yes go to step2 else go to step4 (Condition)
4. Close the jar and run away!

Syntax of do-while loop in C:

do
{
	//statements
}
while(condition)

As the condition is at the end, the statements are executed at least once anyways, then the condition is tested, if it is true, the statements are executed again else the loop terminates.

 

Example of do while loop in cpp

int i=5;
do
{
	printf("i=%d",i);
	i++;
}
while(i<10)

Output:

i=5

i=6

i=7

i=8

i=9

 

Difference between while loop and do-while Loop in C

Consider the following program:

i=10;
while(i<10)
{
        printf("i=%d",i);
	i++;
}
printf("End of while loop,i=%d",i);

Output:

End of while loop,i=10

Do While version

do
{
	printf("i=%d",i);
	i++;
}while(i<10)
printf("End of do while loop, i=%d",i);

Output:

i=10

End of do while loop,i=11

 

Notice that the body of while loop did not execute even once, hence the value of i was not incremented,but in do-while loop, the body was executed for the first time and i was incremented to 11, and then the condition was tested.

Banner to LogicGuns.com

Hide Page Information
while loop in c