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
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
Now another way that we could have completed the challenge is by using a for loop.
Now a for loop works similarly to a while loop, but instead of testing a condition, we’re specifying the
number of times that we want the loop to run. And the syntax looks something like this.
So we have the keyword for and then a set of parentheses.
And the first statement in the parentheses, we’re defining a starting point for the for loop.
So in this case we're starting at 0.
The next step, we're defining the end point,
so we're ending when i
is less than 2.
And the last thing we have to tell the for loop is what is the change,
which direction are we changing i.
Is i increasing by 1 each time,
or you can increase it by 2 or 3, or you can even decrease i
every single time.
So if we take a look back at the while loop that we should now be familiar with,
you can see that the for loop is essentially just a reshuffling of all the elements of the while loop.
So we're changing the while keyword to the for keyword, and then the var creation goes into the parentheses,
defining the starting point,
so we're starting from 1. Then the next thing inside the parentheses is the ending point of the for loop,
so we'll end once i is less than 2.
And finally, if we're using the while loop, we have to include the code that changes i inside the while
loop.
Now it's integrated into the declaration for the for loop so that when we create the for loop we already
define the starting point, the ending point,
how much i is going to change,
and the code that should be carried out every single time the loop runs.
So, as you can see, the for loop is essentially what we would call in programming syntactic sugar.
It just sweetens up the syntax and makes it a little bit easier for developers and programmers to write.
So let's take a look at this for loop in action.
So, in our case, we've got a for loop where we start off with the variable i
that’s equal to 1, and we’re ending when i is less than 2, and we're incrementing by 1 each time.
And inside the for loop we're simply logging the value of i.
So once we get to this line, the computer will run this code and create a variable
i, and it will set it to equal 1.
That's the beginning value.
The next step is it reaches the second part of the for loop where it checks to see whether the current
value of i is less than 2. And 1 is in fact less than 2,
so it proceeds to run the code inside the for loop,
so inside those curly braces. And in this case it console.logs the value of i, which currently we know to
be equal to 1.
Now once it's gotten to the end of the for loop,
this is the point where the third value in that for loop is carried out,
so in this case i is incremented by 1.
So i is now equal to 2. And it's really important that you know when this happens. This happens exactly
at the point where the closing curly braces,
so where the arrow is pointing right now.
And now the code loops back to run the for loop again, and the first thing it does is it checks whether
it should continue running the loop.
So currently i is equal to 2. 2 is not less than 2,
so the conditions are not met and the code exits the for loop and instead goes to the next line.
So if we take a look at the code for our function fizzBuzz, this is how it looks at the moment using
the while loop.
Now if we wanted to use the for loop instead, then we can change the keyword from while to for, and inside
the parentheses, we first declare the variable that we're going to use to count.
So we can cut that from there and paste it here instead.
So now, inside the for loop, we're creating a counter, called count, starting point from 1 and ends at
100.
The next thing that we have to add is how are we going to change the counter.
And in this case we're actually changing it by increasing it by 1 each time.
Now of course you can decrease it by 1 each time as well,
so you could say count minus minus.
And this would work, say for example, if you're counting down from 100 to 1.
So then you would have the opposite code where count starts off being 100
and it decreases by 1 each time until it is no longer greater than 1.
So the for loop is really flexible like that and allows you to count in any direction and increase
your count by 2 or by 3 or by any number you wish really.
And now if I run my code and I call the function fizzBuzz, you can see that it does exactly the same
thing as we had with the while loop, but it's less wordy, more concise, and this is the format that most
programmers will prefer for a use case like this.
Now a lot of students wonder in which cases do I use the while loop and in which cases should I use
the for loop.
Keep in mind that while is essentially checking for a state, so it's while something is true,
so that can be while player one is alive.
And essentially you want to repeatedly run an instruction while the program is in a certain state. But
for the full loop you're essentially trying to iterate.
You're trying to run a piece of code many many times and you're going to use the for loop to define
how many times.
So with practice you'll find that that you'll gravitate toward one or the other type of loops while
you're writing code with different purposes.
So in the next lesson I've got yet another challenge for you.
And you can see the last challenge in the modules as somewhat of a boss level.
It's probably going to be the hardest but the most interesting also.
So it's an interactive challenge complete with code checking.
So once you're ready head over to the next lesson and complete the challenge.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.