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
In the last lecture,
we wrote this for loop to display all the odd numbers
between 0 and 5. And in this lecture, I'm going to show you how to
implement the same logic by using a while loop. Now one
key difference between a y loop and a for loop is that in for loops
the loop variable is part of the loop variable itself.
But in y loops, you have to declare this y variable externally. Let me show
you what I mean. So we start with declaring
a variable like i and set it to 0. Note that this
i we have here is different from the i we have in this for loop,
because this variable is only meaningful and accessible
inside of this for loop. This is called scope, and I'm going to talk about
it later in the course, all I want you to know is that these two
variables are completely different, even though their names are the same.
Now, we have our loop variable initialized to 0.
Next we add a while statement,
in parenthesis we need to add our condition. What is the condition
here? That is i less than or equal
to 5. So I'm going to add that here. Next,
we need to add our statements. So what is the statement that
we want to repeat, here's the statement, we want to display
the odd numbers, so we add that here as well,
and finally, at the end of this while block we need
to increment i. So this is a direct translation
of this for loop into a y loop.
So this is what happens when we execute this code. Initially I
is 0, now in the y loop, first this condition
is evaluated, if this condition is true, then the
body of the y loop will be executed. Again, in the next iteration
the condition is evaluated again, if it's true, the
statements in the y block will be executed, otherwise the while block
will terminate. In the next lecture we're going to look at another kind of
loops in JavaScript.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.