0
What's the difference between for and while loop? (not the bookish answer)
I mean in what kind of situation or program you would prefer while or for and why? ( hope you guys understand what I'm trying to convey)(actually I went through quora too but it went over my head đ )
3 Answers
+ 6
you use for loop if you know how many times you need to loop
https://stackoverflow.com/questions/920645/when-to-use-while-or-for-in-JUMP_LINK__&&__python__&&__JUMP_LINK
+ 2
You use a for-loop when you want to do something for a specific number of times.
For example you have a list with vocabulary and you want to print them all out:
for word in listxy:
print (word)
while-loops you use when you don't know how often you need to do a thing, but you know when it has to stop!
For example if you want a user to input vocabulary into that list.
word = input()
while word:
listxy.append(word)
word = input()
(If the user doesn't give input in the first line, the loop will not even happen once.)
+ 1
thanks Mert Yazıcı and HonFu