answersLogoWhite

0


Best Answer

The difference between "do while" and "do until" is that a "do while" loops while the test case is true, whereas "do until" loops UNTIL the test case is true (which is equivalent to looping while the test case is false).


The difference between a "do ...while" loop and a "while {} " loop is that the while loop tests its condition before execution of the contents of the loop begins; the "do" loop tests its condition after it's been executed at least once. As noted above, if the test condition is false as the while loop is entered the block of code is never executed. Since the condition is tested at the bottom of a do loop, its block of code is always executed at least once.

To further clear your concept on this, understand the syntax and description of the two loop types:

while
The while loop is used to execute a block of code as long as some condition is true. If the condition is false from the start the block of code is not executed at al. The while loop tests the condition before it's executed so sometimes the loop may never be executed if initially the condition is not met. Its syntax is as follows.

while (tested condition is satisfied)
{
block of code
}

In all constructs, curly braces should only be used if the construct is to execute more than one line of code. The above program executes only one line of code so it not really necessary (same rules apply to if...else constructs) but you can use it to make the program seem more understandable or readable.

Here is a simple example of the use of the while loop. This program counts from 1 to 100.


#include

int main(void)
{

int count = 1;

while (count <= 100)
{
printf("%d\n",count);
count += 1; // Notice this statement
}

return 0;

}

Note that no semi-colons ( ; ) are to be used after the while (condition) statement. These loops are very useful because the condition is tested before execution begins. However i never seem to like these loops as they are not as clear to read as the do ...while loops. The while loop is the favorite amongst most programmers but as for me, i definitely prefer the do ...while loop.

do ....while
The do loop also executes a block of code as long as a condition is satisfied.

Again, The difference between a "do ...while" loop and a "while {} " loop is that the while loop tests its condition before execution of the contents of the loop begins; the "do" loop tests its condition after it's been executed at least once. As noted above, if the test condition is false as the while loop is entered the block of code is never executed. Since the condition is tested at the bottom of a do loop, its block of code is always executed at least once.

Some people don't like these loops because it is always executed at least once. When i ask them "so what?", they normally reply that the loop executes even if the data is incorrect. Basically because the loop is always executed, it will execute no matter what value or type of data is supposed to be required. The "do ....while" loops syntax is as follows

do
{
block of code
} while (condition is satisfied);


Note that a semi-colon ( ; ) must be used at the end of the do ...while loop. This semi-colon is needed because it instructs whether the while (condition) statement is the beginning of a while loop or the end of a do ...while loop. Here is an example of the use of a do loop.

include

int main(void)
{

int value, r_digit; printf("Enter a number to be reversed.\n");
scanf("%d", &value); do
{
r_digit = value % 10;
printf("%d", r_digit);
value = value / 10;
} while (value != 0); printf("\n"); return 0;


}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

While LoopDo...While LoopEntry-Controlled LoopExit-Controlled LoopLoop Condition has to be initially

TRUE for body to be executedLoop body will be executed at-least onceInitialization, Condition, Update

Statements are to be written SeparatelyInitialization, Condition, Update

Statements are to be written SeperatelySyntax :

while(condition){

statement...

statement...

}Syntax :

do{

statement...

statement...

} while(condition);

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

A do loop will always execute at least once because the condition check is at the end of the do loop. The loop continues until the condition is false.

do

{

....

....

}

while(a==1);

A while loops checks the condition first and will not execute if the condition is false.

while (a==1)

{

....

....

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

well, the two can be converted into each other:

in: do stmt while (exp);

out: stmt; while (exp) stmt

or: first= 1; while (first exp) { first= 0; stmt }

in: while (exp) stmt

out: if (exp) do stmt while (exp);

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

while() {} the body of this loop will be executed if only expression in brackets are not zero (false). Expression is checked before executing body of loop.

while do {} while() it is very similar to while() loop described above, but the difference is that at least once the body will be executed and after execution expression in brackets is checked.

Examples

while (num < 10) {

printf("%d\n", ++num);

}

int num = 0;

do {

printf("%d\n", ++num);

} while (num < 10);

Both examples produces numbers from 1 to 10.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

difference between c an cpp

** Assuming you mean what's the difference between the two... The while loop will only execute if the condition evaluates as true. The do-while will execute at least once, regardless.

EXAMPLE:

while(DAD_IS_HAPPY) { Give me money }

...in this example, as long as Dad is happy, he'll give me money. If he's not happy, I won't get any money.

do { Give me money

}while(DAD_IS_HAPPY)

in this case, I will get money first, then it checks to see if dad is happy. If he is happy, I'll keep getting money. If he's not happy, I'll still get money that very first time :)

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

There is nothing like do loop.its while loop.
in case of while loop as the condition is given at first,the condition is checked first n then if true the body is executed.But in case of do-while loop.first time the condition is escaped as it is given after the body of the loop.n then same as while loop.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

The while loop evaluates the loop expression first. If it is true, the loop body executes, and then the loop iterates with the loop expression. If the expression is initially false, the loop body will never execute.

The do while loop evaluates the loop body first, then it evaluates the loop expression. If it is true, the loop iterates with the loop body. The loop body will execute at least one time, no matter what the outcome of the loop expression.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

for a do while loop the statements are executed at least one since the statements are executed before they are executed,but for a do loop the statements are first evaluated then executed there they may not necessarily executed

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The while loop tests the condition at the top, while the do while loop tests the condition at the bottom. If the condition is initially false, the while loop will never execute, while the do while loop will execute once.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between while loop and do loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What the difference between pretest loop and posttest loops?

The difference is that pre means before and post means after in Latin so it's tested before or after. :)


Compare Do-While with While Statements?

A Do-While loop looks like this: do { loop body } while (condition); and a While loop looks like this: while (condition) { loop body } The main difference is that the loop body is always run once in the Do-While loop, then the condition is checked to see if the loop should keep running. In a While loop, the condition is checked first, and it will not run the loop body at all if the condition is false.


What is the difference between a loop and a whorl fingerprint?

That a loop is curved and a whorl is shaped like a wave.


What are the types of loops?

There are three forms of loop commonly used in C/C++, the for loop, the while loop and the do-while loop. The for loop is most commonly used whenever an action is going to be performed a set amount of times. For example, to sum every element in an array: for(i = 0; i &lt; arraySize; i++) { sum = sum + array[i]; } The while loop and do-while loop are commonly used to loop until a condition is met. The difference between the two is that the do-while loop goes through one iteration before checking its condition, while the while loop checks its condition before any execution of the loop. Example do-while loop: do { randomNumber = rand() % 10; }while(randomNumber != 6); Example while loop: cout &gt; number; while(number &lt; 0) { cout &gt; number; }


What is difference between for loop and do-while loop?

The do loop is similar to the forloop, except that the expression is not evaluated until after the do loop's code is executed. Therefore the code in a do loop is guaranteed to execute at least once. The following shows a do loop in action: do { System.out.println("Inside do while loop"); } while(false); The System.out.println() statement will print once, even though the expression evaluates to false. Remember, the do loop will always run the code in the loop body at least once. Be sure to note the use of the semicolon at the end of the while expression.

Related questions

What is the difference between a do while loop and a for loop in c?

the counter variable cannot be initialized in while loop before entering into the block.


What is the Difference between while and do while?

Both are programming commands. A do/while loop will execute at least once. A while loop may not execute at all.


What is the difference between if and while loop?

Easy: if-else is not a loop; while, for and do-while are loops.if-else just run once, but do-while run many times.


Difference between for and while loop not in syntactically?

Well 'while' goes like this: while (condition) statement 'for': for (initialize; condition; after-each-loop) statement


Is a while loop or a for loop faster?

No difference.


What is the difference between while loop and for loop in oracle?

You mean PL/SQL? Well, they are different things, read the manual for details.


What is the difference between do and while loops in c?

the main difference b/w do and while loops is that do loop will run atleast once even if condition is not satisfied but while loop will not execute even once if condition is not satisfied . this is bcoz in do loop condition is checked after one execution but in while condition is prechecked.


Difference between while and do while loops in java?

The most important differences are: a. The while loop starts with a condition whereas the condition is the line of code in case of a do while loop b. The do while loop is guaranteed to run the loop body atleast once even if the condition is an impossible to satisfy but such a guarantee is not available with the normal while loop


Open loop and close loop system in injection moluld machine?

loop checking is perform before cable termination..the difference between a close loop and open loop is,tha close loop has a feedback while the open loop has not.


What is the difference between while dowhile?

While: If we can use while statement it will check the condition then proceed further loop statement.DoWhile: If we use dowhile, first execute loop statement then check the condition.


What the difference between pretest loop and posttest loops?

The difference is that pre means before and post means after in Latin so it's tested before or after. :)


What is the difference between while loop and for loop in matlab?

A while loop executes code inside the while block continuously until the said condition is not true. A for loop contains three parts. The first part is carried out prior to the for loop, the middle part is executed by the for loop until it is no longer true, and the final part is performed at the end of each go round of the loop.