Understanding While and Do While Loop Structures

We already discussed about for loop structure in C language in previous article along with an example. We also mentioned that there are three different type of repetitive structure in C programming. Each one has its own importance and advantages.

In this session, we will be discussing about while and do..while loop structures in C language. Idea of these repetitive and control structures will be helpful in all programming language from BASIC to Python to Java. So, you wouldn’t like to minimize this window now.

Below we will also summarize the differences between while and do..while looping statements and their ways of implementation on C.

The while Statement

The while statement is taken as the simplest of all the looping structures in C language. The while statement is used to carry out looping operations, in which a group of statements is executed repeatedly, until some condition is satisfied.

The general form of the while statement is

while (test-condition)

{

Body of loop;

}

The while statement is an entry controlled looping statement. Entry-controlled; because the test-condition tests whether or not to enter into the body of the loop, statements under the body are executed only when the test condition results to true, and the process will be repeated until control is passed to out of the loop, this happens when the condition do not match. The statement can be simple or compound, though it is usually a compound statement.

For example,

/*Program to display consecutive digits 1,2,……………………,10 */

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1;

while(i<=10)

{

printf(“%d\n”,i);

i++;

}

getch();

}

 

The do-while loop

The while loop construct that we discussed above makes a test of condition before the loop is executed. Therefore, the body of the loop may not be executed at all if the condition is not satisfied at the very first attempt. But, the do-while statement test the condition at last of the loop construct thus, do while loop executes a body of statements at least one time, and it executes more if the expression is true.

The general form of do-while loop is

do {

body of the loop;

} while (test-condition);

The statements will be executed repeatedly as long as the value of expression is true. The statement will always be executed at least once, since the test for repetition doesn’t occur until the end of the first pass through the loop.

Remember, do..while statement has semi-colon at the end of the statement, most of beginners are confused with this.

For example,

/*Program to display consecutive digits 1,2,……………………,10 */

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1;

do

{

printf(“%d\n”,i);

i++;

} while(i<=10);

getch();

}

 

Difference between While and Do-While loop

The difference between while and do-while loop is as follows:

While loop Do-while loop
1. The while loop is entry control loop. 1. The do-while loop is exit control loop.
2. The while statement is used to carry out looping operations, in which a group of statements is executed repeatedly, until some condition is satisfied.

 

2. The do-while loop executes a body of statements at least one time, and it executes more if the expression is true.
3. The general form of the while statement is

while (test-condition) {

body of the loop;

}
3. The general form of do-while loop is

do{

body of the loop;

} while (test-condition);

 

4. /*Program to display consecutive digits 1,2,……………………,10 */

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1;

while(i<=10)

{

printf(“%d\n”,i);

i++;

}

getch();

}

 

/*Program to display consecutive digits 1,2,……………………,10 */

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1;

do

{

printf(“%d\n”,i);

i++;

} while(i<=10);

getch();

}

 

I hope it clarifies you the differences between While and Do- While loop statement in C, now you must know where to use it.