All language subtitles for 15. While Loops

af Afrikaans
ak Akan
sq Albanian
am Amharic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Welcome back.

Up until now we've learned that with looping we can use four but there's actually a another way for

us to loop.

And it's called instead of a for loop a while loop and while loop is a little different in that we say

while a condition is happening do something.

So you can see that there's a bit of a difference here.

For example I can say that while zero is less than 50 do something or I can say for example I equals

to zero and I'm going to say while i is less than 50 print I now if I ran this program what do you think

will happen let's find out if I click Run

right.

Do you see that.

I don't know if you can tell but I'm scrolling right now through all of this.

It's looping looping looping looping looping and you see that the programming still running as a matter

of fact I have to stop it because what it was doing is it was looping and looping and looping because

if we look at the code well zero is always going to be less than 50.

So what happened was while this was true we printed AI and then the Python interpreter went back to

line 2 says hey is zero still less than 50.

Yeah.

OK.

Next line print is zero still less than 50.

Yeah ok next line print.

And it kept going keep going keep going.

Now this is what we call an infinite loop.

That is the program doesn't know when to stop.

I mean we're telling it to just keep going forever for eternity.

And I have some safeguards in place here.

But if you did this in code you'd get this infinite loop until your program crashes if you actually

ran this code.

Well it's going to keep running until the machine just doesn't have enough power anymore enough resources

to keep doing this over and over.

Everything has limits right.

So with a while loop we have to be really careful this infinite loop can be very dangerous.

So how can we solve this we had this idea of a break statement and break is.

Well just another key word in Python.

And as it as you may have guessed if I click Run look at that it only runs once as soon as Python sees

break it's going to say All right break out of the wild.

So it's kind of go to line five and it's going to say I got nothing there.

Program's done.

I'm going to exit out of the program but OK.

What if I want this to be looped 50 times.

How can we solve that issue.

Think about well we can just simply say I equals I plus 1 and then make sure that we change this too.

I now what's going to happen if I click Run we stop.

Well at forty nine we've done it 50 times we've lived through it 50 times because we kept incrementing

I over and over so that I each time we came back to the Loop was incremented by 1.

Now a shorthand for this we've seen it before is to do plus equals one.

So that if I run look at that same thing.

So we're also able to break out of the while loop as long as we're able to turn a condition into force.

So you'll see this a lot as well.

So to jump out of a while loop you can either turn the condition false or you can break out of the while

loop.

Now another interesting thing that we can do with a while loop is we can have else blocks.

And here we can say print done with all the work.

And if I click run here at the very end.

Look at that done with all the work.

And this should read like English right.

While I is less than 50 keep looping through here and as soon as we get to the last part where I becomes

50 where 50 is no longer less than 50 Python interpreter is going to say all right I don't care about

anything under there.

Let's go to the next line and I get otherwise.

So in this case we don't have anything else happening.

We can print done with all the work but you're probably asking yourself.

Hold on.

Can't I just not do this and just make sure my indentation is at the end click Run and that that works

right.

Why would I ever want to write else here.

What else is a special case here.

There is one thing that is useful.

That is if we added a for example a break statement here and I click run you'll see that to the else

statement does not print and that is because the else block will only execute if there isn't a break.

That is if the while condition turns to false then it's going to keep going and do the else.

But if there is a break it's going to break out of this whole thing and go to the eighth line.

So a little gotcha here that you won't see else too often next to while but there is that special case

where else is use when you want to make sure that this will be executed only if there isn't a break

statement.

Only if the loop is finished doing whatever it's doing without the breaks then let's take a break and

explore while loops a little bit more.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.