Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So when should you use a while loop and when should you use a for loop.
And it really depends on the problem you're trying to solve.
For example in a for loop I could do four item in let's say a list.
One two three.
And just looking at this code you know right away that it's going to be looped over three times.
Right.
And I could say here print item but he can do this in a while loop as well.
I can say I equals to zero.
And then while I is less than let's say the length of the list that is one two three well in that case
I want you to print I or the item and then increment I by 1 if I run this I see that.
Well technically here we're incrementing the eye which is the value but let's say we had a list up here.
So my list and this list will be 1 2 3.
Well my list can be iterated with a for loop or can be iterated with a while loop and we simply say
my list at index of I and if I run this I get the same results.
But which one do you think is better first off while loops are very flexible.
We can do a lot because we have this conditionals team and we can loop more than three times if we really
wanted to.
So in that case while loops are more powerful but for loops are simpler.
Right.
Like this code reads it really nicely and really well it makes sense.
We just want to loop over something that we already know how many times we want to loop over three times
with a while loop.
We have to create this variable and we have to make sure we remember to increment the variable so we
don't get an infinite loop so with a while loop you need to make sure we remember to halt the loop at
some point.
So my rule is usually this for simple loops or iterating over editable objects for loops are great but
let's say you're not sure how many times you want to loop over something you're not really sure how
long it's going to take for looping.
So you want to say while something is true just keep looping.
For example let's say we're trying to go through an email list that we've collected on a Web site and
for each email list we want to send an email.
Well while the list is still there let's just keep sending emails.
There are many many cases but one of the most useful ways to use the while loop is like this.
It's to say while true do something and make sure that at the end we break hold on.
What's happening here.
I'm saying while true wouldn't that create an infinite loop.
I mean true is always going to be true and you'd be right if you noticed that.
But again we have a break statement here.
So at the end after rerun line 3 it's going to break but now we can do something powerful like input
here.
And if you remember an input is going to ask us for a prompt to enter something so I can say hey input
say something and if I click run it's going to say Hey say something you know let's add semicolon here
or call in here and illustrate that again and look at that it's saying Say something.
I'm on line 3 right now.
So I'm going to say hi
and just see that we just broke out of the loop.
It only asked me once.
Even though this was true as soon as we got to line four I was done.
But what if I remove this and I click run if I say something I say hi.
And then in asking me again.
Hi hi I'm telling Hi.
And it keeps asking me asking me asked me.
And we have an infinite loop.
But the interesting thing here is that we can use conditional logic right.
What if we said response here and collect whatever the responses and say if the response is equal to
let's say by then I'm going to break so that if I run this again I say hi.
Keeps asking me and you know what I'm getting annoyed with you machine I'm going to say bye.
It's going to exit it out for me.
How cool is that.
So while loops are extremely useful for tasks like this where looping can happen for a long time you
don't know how many times it's going to happen.
But this is something that you're just going to have to get used to with practice eventually.
And I promise you this happens where you'll figure out when to use while when to use for loop but at
the end of the day use whatever solves your problem.
I'll see in the next one by.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.