In this article while and do while difference we give the information about  While is an entry controlled loop and Do-while is exit controlled loop.

while and do while difference:

Sr. No. While Loop Do-While Loop
1 While is an entry controlled loop Do-while is exit controlled loop
2 Syntax:

while(test condition)

{

Body of the loop;

}

statement -x

Syntax:

do

{

Body of the loop;

} while(test condition);

statement -x

3 In the while loop the condition is checked first.

 

The condition is checked later in the do while loop.

 

4 The while loop executes the code only if the condition is true.

 

The do while loop executes the code once whether the condition is true or false.

 

5 Semicolon (;) is not used in while loop.

 

The semicolon (;) is used at the end of the do while loop.

 

6 Program:

// display 1 to 10 numbers

#include<stdio.h>

#include<conio.h>

void main()

{

int i;

clrscr();

while(i<=10)

{

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

i++;

}

getch();

}

O/P:

1

2

3

4

5

6

7

8

9

10

Program:

// display 1 to 10 numbers

#include<stdio.h>

#include<conio.h>

void main()

{

int i;

clrscr();

do

{

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

i++;

} while(i<=10);

getch();

}

O/P:

1

2

3

4

5

6

7

8

9

10

while and do while difference:

While Loop:

The While statement is the simplest of all the looping structures in C. We have used while in many of our earlier programs.

while (test condition)
{
Body of the loop.
}
While is an entry-controlled loop statement. The test-condition is evaluated and if the condition is true, the main part of the loop is executed. After the body is executed, the test-condition is re-evaluated and if it is true, the body is re-executed.

Do…While Statement:

do
{
Body of the loop
}
while (test-condition);

The while loop construct, which we discussed in the previous article, tests the condition before the loop is executed. Therefore, if the conditions are not met on the first attempt, the main part of the loop cannot be executed at all. In some cases it may be necessary to execute the main part of the loop before testing it. Such situations can be controlled with the help of Do…while statement.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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