When do I use a for loop and when do I use a while loop?


All for loops can be written as while loops, and vice-versa. Just use whichever loop seems more appropriate to the task at hand. 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 upon a condition other than the number of times it runs, you should use a while loop.


Last updated October 9. 2017

Was this article helpful?
10 out of 10 found this helpful
Have more questions? Submit a request