Afrikaans
Akan
Albanian
Amharic
Arabic
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
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
Alright now let's
take a look at the third kind of loop we have in JavaScript.
That is a do-while loop. Do-while loops
are very similar to y loops, but they're slightly different. It's
easier to show you in code. So I'm going to rewrite this logic
using a do-while loop. Just like the y loops, we have to declare
our loop variable externally, so we declare i and
initialize it to 0. Okay, now if we save the changes,
we're going to get an error identifier has already been declared.
Because I have declared i here, so I can not redeclare
it here. This is different than the i that we have in the for loop,
again, this is all about scoping, and I'm going to talk about that later in the course.
So, to make this work I'm going to temporarily
comment out these few lines, and rewrite this
logic using a do while loop. So we add the do statement
do statement here, then a code block, in this block we should
have our statements. So I'm going to borrow them
from our while loop, copy these, paste them here,
and then finally at the end
of this block we add the while statement along with
our condition. That is i less than or equal to 5,
followed by a semi colon. Now you might be wondering
what is the difference between a while loop, and a do-while loop,
do-while loops are always executed at least once.
Even if this condition evaluates to false. Let me show you
what I mean. So I'm going to temporarily comment out these few
lines. And bring back our while loop.
Also, we don't need this for block for now, so let's comment it out,
now if you save the changes, we get 1, 3, 5 on
the console. However, if I change i
to 9, we are not going to see anything, because the first time
we try to execute this while loop, this condition evaluates
to false. So these statements are never executed.
So save the changes, look, there's nothing in the console. So in
while loops, this condition is evaluated ahead of time.
At the beginning of every iteration. In contrast
in do-while loops,
this condition is evaluated at the end. And
that means these statements are always executed
at least once. Even if the condition is false.
So let's try this, I'm going to comment out this while loop,
and change i to 9, just like before.
Save the changes, we get 9 on the console,
why? Because in our do-while loop here,
on line 15, we check to see if this is an odd-number it is,
and display it on the console. Next, we increment i by 1, so i,
is 10. Then, the condition is evaluated, of course it's
false. So our loop will terminate. Now, realistically
we're not going to use this do-while a lot in programming,
there are situations you may want to use this, but in practical terms,
most of the time you will be using a for or while loop. Just
be aware of the difference between a while loop and a do while loop.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.