Next we have the for loop.
This one is quite flexible.
It exists in different programming languages, so if you've ever programmed in any language, you should
be pretty familiar with the for loop.
Anyway, let's go through it.
Now this one is actually quite flexible, yet often it's used to do a simple operation like repeat something
5 or 10 times.
So it has the most complex syntax actually from all the other loops.
And let's go through it.
So it starts with the for keyword.
And there are actually three expressions inside.
So there is this first expression that is always run once only once at the beginning of the loop.
This is often used to initialize the variable that counts the number.
If you are just trying to do something five times.
We're gonna see that in a second.
And the next step is always to evaluate the condition.
That's the second expression.
And if this condition is true, then we just execute the body of the for loop, which is some statements
or no statements.
If this condition is false, we immediately well basically we are done executing the loop.
But there is one more expression.
So if the condition was true and statements were executed, then we unconditionally run this third expression.
And this happens all the time.
So this third expression is always run after the statements in the for loop body are executed.
And then we go back to the condition to the second expression to see if it's still true.
And again, we decide if we should run the body of the for loop again, or just be done with the loop
already.
Now let's see that in our real example.
So let's start by creating a file for our exercise.
Let's call it for PHP.
And let's get started with the PHP tag.
What I'm going to do here is a rocket launch countdown.
So a funny little exercise.
So first we're going to echo the rocket launch countdown so people know what they are waiting for.
And then let's define the for loop.
As usual with all those control structures we've got parentheses right after the keyword.
And now I set about three expressions.
So the first one runs only once.
Ones, so it's often used to initialize a variable that will be used to control this loop.
So we can initialize it with ten.
So this here is defining a variable.
Keep in mind.
This variable is not available outside this for loop.
We're going to talk about that more later on.
And then we've got a condition.
So this is the condition that will be run every single time to.
Determine if the body of this for loop should be run or not.
So this is this E which is also.
Often the name of the variable used in the for loop.
That's just a convention.
If it's more than.
Zero then we should continue.
And this third expression is something that is always run after the body of.
This loop is executed.
So can you think what this should be?
What would make all this here actually make sense?
Well, this is decreasing this I variable value by one.
So to decrease the value of a variable by one, you can use this minus minus operator on the variable
which will just decrease the value by one.
Next we've got the curly braces which would be optional if you would only have one statement inside
this for loop body.
But we're going to have more.
So the first thing inside is to just say what's the current counter value.
So let's concatenate that with three dots like this.
And we need to check if maybe it's the lift of time.
So we're going to use the if statement and we're going to verify if one equals I.
That's another thing you might keep in mind in comparisons that you add to if statements or any other
control structures.
So basically the conditions that you create that it is always safer to compare a value on the left to
the variable on the right.
Why?
Well, because if you would do it the other way.
You might forget one equality sign.
And this here is a completely different statement.
This is assigning one to this variable every single time.
And trust me that this just happens all the time, even to the experienced engineers.
And you can't assign anything to a value.
and this would immediately throw an error if you would accidentally use this assignment instead of comparison.
Okay, so when this is true, we just say that it's the liftoff time.
Let's add some funny symbol.
Okay, let be a rocket and a new line.
And then let's just sleep for one second.
So we have some delay between all those messages showing up.
Okay, we are ready to try this out.
So now in the terminal just type PHP four dot PHP.
There we have it.
The countdown is happening.
Almost there.
And there is the liftoff.
And the program has finished.
So if I would be to summarize the for loop and maybe how it differs from the while loop.
There are some obvious differences, but we can also say that those loops are pretty similar.
They do the same thing.
They let you execute some code a couple of times.
So here there is just one expression, which is a condition.
And the for loop is maybe more complex.
And the use cases, well I think that this just feels more natural.
Uh, given the case that you need to handle which of those loops to use.
So I wouldn't go with any generic recommendations.
There are many options.
So we would be just choosing the best options.
That just feels more natural for the job.
But eventually to actually have this instincts of which loops you should be choosing, well, you just
need to write a lot of code.