Wednesday, January 13, 2016

Difference between FOR & WHILE Loop

For Loop:

It is a fixed repetition loop. It will run the given number of times no matter how the body of for loop code changes the values of the variables.

If you have a constant, you can run the For Loop for that many times. You can change the value of the constant.

The counter value increases or decreases depending on the for loop min, max, incremental or decremented values automatically as stated per requirement -- no separate statement is made within the body of the loop.

A separate variable is needed most times to run a For loop.

While Loop:

While loop can have various conditions and they can be joined. The control enters the loop body only when the condition is met. If the condition is not met, the control goes to the statement following the end of the while loop body. A statement within the while loop changes the criteria and that actually moves the control out and to the statements that are following the end of while loop body.

No comments:

Post a Comment