in general a while loop is used if you want an action to repeat itself until a certain condition is met i.e. if statement. An for loop is used when you want to iterate through an object.
In general, you should use a for loop when you know how many times the loop should run. If you want the loop to break based on a condition other than the number of times it runs, you should use a while loop.
The for loop simplifies the complex problems into simple ones. It enables us to adjust the flow of the program so that instead of writing the same code over and over, we can repeat similar code a limited number of times.
What is the major difference between for loop and while loop? The major difference between for loop and while loop is that in the case of for loop the number of iterations is known whereas in the case of the while loop number of iterations is unknown and the statement will run until the condition is proved false.
In your case, you don't gain much besides one less line of code in the for
loop.
However, if you declare the loop like so:
for(int i = 0; i < x; i++)
You manage to keep i
within the scope of the loop, instead of letting it escape to the rest of the code.
Also, in a while loop, you have the variable declaration, the condition, and the increment in 3 different places. With the for loop, it is all in one convenient, easy-to-read place.
Last thought:
One more important note. There is a semantic difference between the two. While loops, in general, are meant to have an indefinite number of iterations. (ie. until the file has been read..no matter how many lines are in it), and for loops should have a more definite number of iterations. (loop through all of the elements in a collection, which we can count based on the size of the collection.)
There is one reason to choose for
over while
: Readability.
By using a for
loop, you're expressly saying your intent is to perform some type of operating that requires an initialization, a "step" operation, and a completion condition.
With while
, on the other hand, you're only saying you need the completion condition.
I use 'while' when I don't necessarily need a counter, for example when you are reading from a file and you are waiting to reach EOF. In this case 'for' may not be the best alternative. When you are going through items on an array I'd rather use 'for' because it conveys intent better.
The one notable difference between a for() and while() loop is that a "continue" statement in a while() loop will branch to the top of the loop, while one in a for() loop will branch to the third part of the for() clause [the one after the condition, usually used to bump variables].
As a teacher I have been pondering this in various shapes and forms; my suggestion always boils down to
for
-loops are for counting. counting up, counting down.while
/ do-while
constructs are for all other conditions. c!=EOF
, diff<0.02
, etc.
Iterators/Enumerators are counters very suitable for for-loops.i.e. your int=0; while( ... )
is hideous to my eyes.
Why choose Coke over Pepsi?
Between a WHILE and FOR loop, you can use them interchangeably. To be a purist, you could make your decision base on the nature of the conditions. If you're performing a count-based loop, then a FOR loop would make the most sense.
for( cur = 0; cur < myList.Length; cur++ ){
doSomething( myList[cur] );
}
If you're performing a logic-based loop, then a WHILE would make for the cleanest implementation
Iterator i = myObject.getIterator();
while( i.hasNext() ){
doSomething( i.next() );
}
As you say, the reasons are semantical and aesthetical, rather than performance (their compiled code tends to be very similar).
That said, for
is usual in cycles which have a known number of iterations, whereas while
implies that the condition isn't necessarily correlated to what you're doing inside the cycle, or that you don't know at the beginning how many iterations there will be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With