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.
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.
old:
do statement while (condition);
new#1:
statement
while (condition) statement
new#2:
int loopflag= 1;
while (loopflag) {
statement
loopflag= condition;
}
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.
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.
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.
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.
Because you have to repeat something. (Or you can use while-loop, too.)
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).
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
A counted loop. Typically we use a for loop for counted loops: // loop 10 times... for (int i=0; i<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<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<11);
You can use a for loop whenever you can use a while loop; it's the same.
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<=n;)
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.
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'
Because you have to repeat something. (Or you can use while-loop, too.)
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).
A counted loop. Typically we use a for loop for counted loops: // loop 10 times... for (int i=0; i<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<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<11);
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<=8; i++) { n=n+5; } while loop: int i=5, n=2; while(i<=8) { n=n+5; i++; }
Yes, it's easy:Old: while (expression)statementNew: for (; expression;)statement
1) use for loop 2) do while loop
do WHILE loop defination
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.