answersLogoWhite

0


Best Answer

Generally speaking a for loop looks like this:

for(Initialization;condition;increment)

{

Do Stuff

}

whereas a while loop looks like this:

while(condition)

{

Do Stuff

}

Before the while loop there should be some initialization and somewhere in the Do Stuff section there should be statements that change the condition. Those statements are analogous to the increment section of the for loop.

User Avatar

Wiki User

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

Wiki User

11y ago

A for loop is typically used whenever a sequence needs to be performed a pre-determined number of times using a counter variable and a conditional expression. It is quite similar to a while loop except a while loop only has a conditional expression and is typically used for infinite loops. A do-while loop is similar to a while loop except that the conditional expression is evaluated at the end of the loop rather than at the beginning. Thus a do-while loop is guaranteed to execute at least once, whereas a for or while loop may not execute at all.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

old:

do statement while (condition);

new#1:

statement

while (condition) statement

new#2:

int loopflag= 1;

while (loopflag) {

statement

loopflag= condition;

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Your question is to open. While loops are used when something equals something such as

While x=80

but a for loop will continually go on until the loop finishes. So their is no possible way to convert a while to a for loop.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Use a while loop when you need to test the controlling conditional expression before each iteration begins. Use a do loop when you need to test the controlling conditional expression after each iteration.

The main difference between the two types of loop is that a do loop always performs at least one iteration whereas the while loop may not iterate at all.

Whenever possible, prefer a while loop to a do loop because a while loop has the conditional expression up front where it is easily seen and therefore makes your code that much easier to read.

This answer is:
User Avatar

User Avatar

Wiki User

16y ago

for(i=0, i<10, i++) { ... } or int i=0 while(i<10) {i++ ... } yes, but as you can see the for loop simplifies the while loop in my example above.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When should use a while loop over a do loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why you need a for loop when you have already while loop or do while loop?

We need a for loop because the while and do-while loops do not make use of a control variable. Although you can implement a counter inside a while or do-while loop, the use of a control variable is not as self-evident as it is in a for loop. Aside from the use of a control variable, a for loop is largely the same as a while loop. However, it is quite different to a do-while loop, which always executes at least one iteration of the loop before evaluating the conditional expression. In a for and while loop, the conditional expression is always evaluated before entering the loop, which may result in the loop not executing at all.


Why you use for loop inside switch statement?

Because you have to repeat something. (Or you can use while-loop, too.)


How do you decide the use of one of the three loops in C for a given problem?

The for loop should be used when there is a progression over the members of an array, or where there is a clear initialization, termination-check, and increment. If the block simply has to execute until some condition occurs, use a while loop. The do loop should only be used when the loop must execute at least once, and even then sparingly. It is harder to understand, since the test occurs at the end (it is more like a simple goto).


What is main different between do while and while?

If you want to execute a statement which is in while loop at least one time you can use do- while loop. this is why because initially first statements will be executed then condition will be checked. but in case of while first condition will be check then statements will be executed


What type of loop uses Boolean expression to control the number of times that it repeats a statement set of statements?

A counted loop. Typically we use a for loop for counted loops: // loop 10 times... for (int i=0; i&lt;10; ++i) { // ... } We can also use while and do-while loops to do the same thing, however a for loop provides all the information up front where it belongs and we can localise the control variable. With while and do-while loops, the control variable must be declared outside the loop, and the increment is usually specified at the end of the loop. This makes while and do-while loops harder to read because the information that controls the loop is separated: // loop 10 times... int i = 0; while (i&lt;10) { // ... ++i; } A do-while loop is similar to a while loop, but the control expression is placed at the end of the loop thus the loop always executes at least once. This also upsets the logic of a counted loop because the control variable is off-by-one. // loop 10 times... int i = 0; do { // ... ++i; } while (i&lt;11);

Related questions

For loop -uses?

You can use a for loop whenever you can use a while loop; it's the same.


Is it possible to use a for loop in place of a while loop?

Yes. while loop consist of only condition statement to make for loop look as while loop we can use syntax shown below: for(;condition;) eg: for(;i&lt;=n;)


Why you need a for loop when you have already while loop or do while loop?

We need a for loop because the while and do-while loops do not make use of a control variable. Although you can implement a counter inside a while or do-while loop, the use of a control variable is not as self-evident as it is in a for loop. Aside from the use of a control variable, a for loop is largely the same as a while loop. However, it is quite different to a do-while loop, which always executes at least one iteration of the loop before evaluating the conditional expression. In a for and while loop, the conditional expression is always evaluated before entering the loop, which may result in the loop not executing at all.


What is an infinite loop in c plus plus?

An infinite loop is one sequence of commands that just repeats over and over again forever. When it comes to creating an infinite loop you can use the: for do while and do statements. using the keywords 'true'


Why you use for loop inside switch statement?

Because you have to repeat something. (Or you can use while-loop, too.)


How do you decide the use of one of the three loops in C for a given problem?

The for loop should be used when there is a progression over the members of an array, or where there is a clear initialization, termination-check, and increment. If the block simply has to execute until some condition occurs, use a while loop. The do loop should only be used when the loop must execute at least once, and even then sparingly. It is harder to understand, since the test occurs at the end (it is more like a simple goto).


What type of loop use a Boolean expression to control the number of times that it repeats a statement set of statements?

A counted loop. Typically we use a for loop for counted loops: // loop 10 times... for (int i=0; i&lt;10; ++i) { // ... } We can also use while and do-while loops to do the same thing, however a for loop provides all the information up front where it belongs and we can localise the control variable. With while and do-while loops, the control variable must be declared outside the loop, and the increment is usually specified at the end of the loop. This makes while and do-while loops harder to read because the information that controls the loop is separated: // loop 10 times... int i = 0; while (i&lt;10) { // ... ++i; } A do-while loop is similar to a while loop, but the control expression is placed at the end of the loop thus the loop always executes at least once. This also upsets the logic of a counted loop because the control variable is off-by-one. // loop 10 times... int i = 0; do { // ... ++i; } while (i&lt;11);


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

when use for loop user can assign the value of the variable inside the loop, while checking the condition. but in while loop he has to assign it out side of the loop. like for loop: for(i=5, n=2; i&lt;=8; i++) { n=n+5; } while loop: int i=5, n=2; while(i&lt;=8) { n=n+5; i++; }


Is possible to use a for loop in place of a while loop?

Yes, it's easy:Old: while (expression)statementNew: for (; expression;)statement


How can you repeatedly execute a code in java script?

1) use for loop 2) do while loop


How do you use switches in c?

do WHILE loop defination


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.