In addition to the while loop we also have far loops in p s p for loops I used to execute some cold
a specific number of times when you know in advance how many times you want the code to run.
Follow up.
It's based on a counter the counter is a variable that controls how many times the cold inside the loop
is executed.
When we create a far loop First we initialize the counter to its initial value then we specify a test
for it.
If this tests result is true then the cold inside the block is executed.
If it's false then the loop ends.
Finally we add a statement that will change the counters value after each time the code book is executed.
Let's look at an example.
We'll print out the numbers from 1 to 10 first with initialize the counter and you can call the counter
variable whatever you like.
But i is a typical name for the counter in a followup.
Then I will add the condition separating it with a semicolon which says to run the cold while the counter
is less than equal to 10.
Then finally they will add a statement that will add 1 to the value of the counter.
After each iteration of the loop and then finally in the cold block will just print out the value of
the counter.
When we run this in the browser we get the numbers from 1 to 10 printed out.
Let's go through what happened.
The file loop starts by initializing the counter to 1.
It then checks the condition.
Which is true because one is less than equal to 10.
So the code in the block executes.
Then the third statement in the follow up is executed.
Incrementing the counter by 1.
The condition is checked again and it's still true.
So the cold in the block executes then the counter is incremented again.
The condition is checked.
It's true so the code in the block executes and so on until the counter is incremented to 11 then the
condition is false.
So the loop finishes.
We can make a small improvement to the cold here with incrementing the counter by one by adding one
to its value and then assigning this back to the counter variable.
However instead of doing this in P HP we have a combined operator that allows us to express this more
succinctly.
So instead of doing this we can just use this combined operator to add one to the value of the variable.
So let's change this expression to use the combined operator like that and then in the browser
the result hasn't changed and we see the 10 numbers printed out.
We can improve this even further as there's an upper 3 to in p h.
P there will increments of value far as putting two plus signs after a variable name will increment
a value by one.
So let's change this again to use this operator like that.
And then in the browser we get the same results.
It's very common to use this increment updater like this in a file doop.