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 😅)

5th Oct 2018, 4:12 PM
partha
partha - avatar
3 Answers
5th Oct 2018, 4:16 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 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.)
5th Oct 2018, 10:02 PM
HonFu
HonFu - avatar
+ 1
6th Oct 2018, 12:30 AM
partha
partha - avatar