A for loop allows us to execute a block of code repeatedly they come in handy if you want the same code
to run over and over again each time with a different value.
Let's take a look at an example to illustrate how for loops work.
The objective of the for loop we create will be to output the values zero to 10 on separate lines in
our HMO document as shown here
so let's go ahead and get started.
The first thing we need to do is declare the variable that will contain our initial values
we'll call this variable count.
Now we can create our for loop to do so type the following block of code
and that's it.
In this block of code we first established the for loop.
Next we define the parameters of the for loop.
We've set the initial value for variable count to 0.
We then instruct the script that the value should increment by 1 up to a maximum of 10 the document
dot write functions simply output each value in the loop separated by a line break.
So let's go ahead and save this file and tested in our web browser to make sure it works and we can
see here that each value is output it onto a separate line just as we instructed it to do.
Now let's see what would happen if we were to change the value of our account variable to a different
value.
I'll change it to 3 save the file and refresh.
Now we can see that our initial value starts at 3 and increments up to 10
we can increase our maximum value by changing 10 to 20 for example.
And now it'll keep looping until it reaches 20 instead of 10.