- 1
What are the differences and similarities between for and while loops?
Additional question: When is for used and when while?
2 odpowiedzi
+ 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
+ 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".|