Do while Loop in C Language

There is another loop control structure which is very similar to the while statement, called as the Do While Loop in C Language. The only difference is that the expression which determines whether to carry on looping is evaluated at the end of each loop.

The syntax for Do while Loop in C Language

do
{
statement(s);
}

while(test condition);

In the do-while loop, the body of the loop is executed at least once before the condition is evaluated. Then the loop repeats body as long as the condition is true. However, in while loop, the statement doesn’t execute the body of the loop even once, if the condition is false. That is why the do-while loop is also called exit-control loop.

Do while Loop in C
Do while Loop

Program to illustrate Do while Loop in C Language

Write a program to print first ten even natural numbers.

/* Program to print first ten even natural numbers */

#include <stdio.h>

main()

{

int i=0;

int j=2;

do {

printf(“%d”,j);

j =j+2;

i=i+1; } while (i<10);

}

OUTPUT

2 4 6 8 10 12 14 16 18 20

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
?>