What Is Basic Difference Between While Loop And Do-while Loop?

9

9 Answers

AMOL NAKHWA. Profile
AMOL NAKHWA. answered
1. In While loop the condition is
tested first and then the statements are executed if the condition turns out to
be true.
In do while the statements are executed for the first time and then the conditions
are tested, if the condition turns out to be true then the statements are
executed again.

2. A do while is used for a block
of code that must be executed at least once.
These situations tend to be relatively rare, thus the simple while is more commonly
used.

3. A do while loop runs at least once
even though the the condition given is false
while loop do not run in case the condition given is false

 

4. In a while loop the condition is
first tested and if it returns true then it goes in the loop

In a do-while loop the condition is
tested at the last.

 

5. While loop is entry control loop
where as do while is exit control loop.

 

6. Syntax:

 

while loop  :

 while (condition)

{

Statements;

}

 do while loop  :

Do

{

Statements;

}while(condition);

 
Anonymous Profile
Anonymous answered
In a while loop the condition is first tested and if it returns true then it goes in the loop, while in a do-while loop the it enters the loop 1st and does the 1st iteration and then does the checking at the end.... If at the end in a do-while loop it returns true the 2nd iteration follows , but if it returns false then it goes out of the loop...
Anonymous Profile
Anonymous answered
In-case of while loop the given condition is checked and the statement of loop will be executed if the condition found to be true.
But At Do-while loop the given statement will be executed atleast once and thereafter the condition will be checked. And if condition founds false the execution stops.
This is the BAsic difference between while loop and do-while loop.

And I hope this will help you.
See you.
raaga Profile
raaga answered
In general both the while loop and do-while loop are iterative control structures in any programming language. Both are conditional loops because these are based on conditions (Boolean expressions). The value of Boolean expression (true /false) determines whether the loop exits or not.    But there is a little difference in their way of iterating.  The basic difference is in the time of checking the condition during execution of loop. In while loop the condition is checked at the start, if it is true then statements enclosed in the loop structure are executed , otherwise the loop exits and control transfers to the statements following this loop and  this process continues until the condition becomes false.  As:                                    While (condition)                        {                            statement1;                            statement2;                                      .                                      .                          }  Statements following the loop;      But in the case of do-while loop condition is checked at the end of structure (i.e., at least one time the statements enclosed in this loop structure are executed before checking the condition)  After executing the loop statements at least once, the loop condition is checked; if it is true then the loop body is executed again otherwise loop exits. As:                          Do                          {                              statement1;                              statement2;                                      .                                      .                            }                            While (condition)  Statements following the loop;  Thus ,their syntax shows clearly how these two loops are different from each other.
gaurav mehta Profile
gaurav mehta answered
While loop is entry control loop where as do while is exit control loop.      While=> condition of loop is checked first befor executing the body of loop.    Do while=>  condition of loop is checked after executing the body of loop.i.e loop execute at least once.  It is mainly used in menu like programmes where all the available choices are printed at least once.
Anonymous Profile
Anonymous answered
In while loop first condition is check if it is not true than iteration is takes palace but in do while loop after first iteration condition is check either it is true or false.
Khadija Brohi Profile
Khadija Brohi answered
@ In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement.

@ a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement.

@ a do while loop, sometimes just called a
do loop, is a control flow statement that allows code to be executed repeatedly
based on a given Boolean condition. Note though that unlike most languages,
Fortran's do loop is actually analogous to the for loop.
Anonymous Profile
Anonymous answered
The while loop statement while(true) will always evaluate to true and the while loop will run an indefinite number of times.

The do while ... Loop construct is similar to the while loop construct both iterate until the specified loop condition becomes false however , in the do... While loop the body of the loop is executed at least once condition is evaluated for subsequent iterations .
Anonymous Profile
Anonymous answered
In case of while loop first the control checks the condition given inside while ,and it executes only if the condition is true,
And in case of do-while the control first executes the one time and then checks the conditions if it is true control go further else comes out. Naveen Rathor

Answer Question

Anonymous