What are the differences and similarities between for and while loops? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

What are the differences and similarities between for and while loops?

Additional question: When is for used and when while?

17th Jul 2016, 9:06 AM
Benjamin Doppler
2 Answers
+ 9
Difference: - while loop is condition dependent and does not necessarily need the content to be evaluated - for loop requires content to iterate over Similarity: - both can iterate over a list or structure, but you must define your content to iterate through beneath the while condition whereas for loops define the content index and type at start
17th Jul 2016, 9:45 AM
James Lanzon
James Lanzon - avatar
+ 7
Their similarity is that both "while" and "for" are used for repeating a loop. Their difference is that we use "for" when we know how many times we need to re-run the loop. When we don't know how many times are needed we use "while".|
22nd Jan 2017, 8:36 PM
iamaprogrammer