WEBVTT

00:00.200 --> 00:02.660
Next we have the for loop.

00:02.690 --> 00:05.090
This one is quite flexible.

00:05.120 --> 00:12.020
It exists in different programming languages, so if you've ever programmed in any language, you should

00:12.020 --> 00:14.780
be pretty familiar with the for loop.

00:14.810 --> 00:16.730
Anyway, let's go through it.

00:16.760 --> 00:25.040
Now this one is actually quite flexible, yet often it's used to do a simple operation like repeat something

00:25.070 --> 00:27.290
5 or 10 times.

00:27.320 --> 00:33.530
So it has the most complex syntax actually from all the other loops.

00:33.530 --> 00:35.180
And let's go through it.

00:35.180 --> 00:37.250
So it starts with the for keyword.

00:37.250 --> 00:41.000
And there are actually three expressions inside.

00:41.000 --> 00:50.540
So there is this first expression that is always run once only once at the beginning of the loop.

00:50.870 --> 00:57.050
This is often used to initialize the variable that counts the number.

00:57.050 --> 01:00.020
If you are just trying to do something five times.

01:00.020 --> 01:01.730
We're gonna see that in a second.

01:01.820 --> 01:06.500
And the next step is always to evaluate the condition.

01:06.530 --> 01:09.050
That's the second expression.

01:09.050 --> 01:17.660
And if this condition is true, then we just execute the body of the for loop, which is some statements

01:17.660 --> 01:19.550
or no statements.

01:19.910 --> 01:27.620
If this condition is false, we immediately well basically we are done executing the loop.

01:27.650 --> 01:30.440
But there is one more expression.

01:30.440 --> 01:41.060
So if the condition was true and statements were executed, then we unconditionally run this third expression.

01:41.060 --> 01:43.190
And this happens all the time.

01:43.190 --> 01:51.620
So this third expression is always run after the statements in the for loop body are executed.

01:51.620 --> 01:57.930
And then we go back to the condition to the second expression to see if it's still true.

01:57.930 --> 02:05.850
And again, we decide if we should run the body of the for loop again, or just be done with the loop

02:05.850 --> 02:06.660
already.

02:06.870 --> 02:10.320
Now let's see that in our real example.

02:11.850 --> 02:15.420
So let's start by creating a file for our exercise.

02:15.420 --> 02:17.400
Let's call it for PHP.

02:18.960 --> 02:21.540
And let's get started with the PHP tag.

02:21.540 --> 02:24.990
What I'm going to do here is a rocket launch countdown.

02:24.990 --> 02:27.870
So a funny little exercise.

02:27.870 --> 02:37.860
So first we're going to echo the rocket launch countdown so people know what they are waiting for.

02:37.980 --> 02:41.190
And then let's define the for loop.

02:41.190 --> 02:48.660
As usual with all those control structures we've got parentheses right after the keyword.

02:48.660 --> 02:51.750
And now I set about three expressions.

02:51.750 --> 02:55.470
So the first one runs only once.

02:55.470 --> 03:02.670
Ones, so it's often used to initialize a variable that will be used to control this loop.

03:03.000 --> 03:06.540
So we can initialize it with ten.

03:06.540 --> 03:09.480
So this here is defining a variable.

03:09.510 --> 03:10.290
Keep in mind.

03:10.320 --> 03:14.460
This variable is not available outside this for loop.

03:15.030 --> 03:18.150
We're going to talk about that more later on.

03:18.180 --> 03:20.850
And then we've got a condition.

03:20.850 --> 03:25.710
So this is the condition that will be run every single time to.

03:25.740 --> 03:30.270
Determine if the body of this for loop should be run or not.

03:30.300 --> 03:34.830
So this is this E which is also.

03:34.860 --> 03:38.820
Often the name of the variable used in the for loop.

03:38.820 --> 03:40.500
That's just a convention.

03:41.460 --> 03:42.870
If it's more than.

03:42.900 --> 03:45.090
Zero then we should continue.

03:45.090 --> 03:53.430
And this third expression is something that is always run after the body of.

03:53.460 --> 03:55.510
This loop is executed.

03:55.510 --> 03:59.770
So can you think what this should be?

04:00.430 --> 04:05.740
What would make all this here actually make sense?

04:05.770 --> 04:11.740
Well, this is decreasing this I variable value by one.

04:11.740 --> 04:19.600
So to decrease the value of a variable by one, you can use this minus minus operator on the variable

04:19.600 --> 04:22.900
which will just decrease the value by one.

04:23.350 --> 04:31.060
Next we've got the curly braces which would be optional if you would only have one statement inside

04:31.060 --> 04:32.800
this for loop body.

04:32.800 --> 04:34.690
But we're going to have more.

04:35.260 --> 04:42.430
So the first thing inside is to just say what's the current counter value.

04:42.640 --> 04:48.970
So let's concatenate that with three dots like this.

04:48.970 --> 04:56.020
And we need to check if maybe it's the lift of time.

04:56.410 --> 05:01.840
So we're going to use the if statement and we're going to verify if one equals I.

05:02.110 --> 05:10.300
That's another thing you might keep in mind in comparisons that you add to if statements or any other

05:10.300 --> 05:11.440
control structures.

05:11.440 --> 05:21.220
So basically the conditions that you create that it is always safer to compare a value on the left to

05:21.250 --> 05:22.840
the variable on the right.

05:22.840 --> 05:23.860
Why?

05:23.890 --> 05:27.670
Well, because if you would do it the other way.

05:30.040 --> 05:33.550
You might forget one equality sign.

05:33.550 --> 05:37.720
And this here is a completely different statement.

05:37.720 --> 05:42.400
This is assigning one to this variable every single time.

05:42.400 --> 05:47.290
And trust me that this just happens all the time, even to the experienced engineers.

05:47.290 --> 05:50.620
And you can't assign anything to a value.

05:50.620 --> 05:58.810
and this would immediately throw an error if you would accidentally use this assignment instead of comparison.

05:59.020 --> 06:04.720
Okay, so when this is true, we just say that it's the liftoff time.

06:06.070 --> 06:09.580
Let's add some funny symbol.

06:12.580 --> 06:16.330
Okay, let be a rocket and a new line.

06:18.550 --> 06:22.240
And then let's just sleep for one second.

06:22.240 --> 06:26.350
So we have some delay between all those messages showing up.

06:26.380 --> 06:28.720
Okay, we are ready to try this out.

06:29.650 --> 06:33.760
So now in the terminal just type PHP four dot PHP.

06:33.790 --> 06:34.570
There we have it.

06:34.600 --> 06:36.430
The countdown is happening.

06:40.060 --> 06:41.050
Almost there.

06:41.050 --> 06:43.900
And there is the liftoff.

06:43.900 --> 06:46.090
And the program has finished.

06:46.420 --> 06:52.680
So if I would be to summarize the for loop and maybe how it differs from the while loop.

06:53.160 --> 06:59.310
There are some obvious differences, but we can also say that those loops are pretty similar.

06:59.310 --> 07:00.570
They do the same thing.

07:00.600 --> 07:03.690
They let you execute some code a couple of times.

07:03.690 --> 07:07.170
So here there is just one expression, which is a condition.

07:07.170 --> 07:10.380
And the for loop is maybe more complex.

07:10.380 --> 07:16.470
And the use cases, well I think that this just feels more natural.

07:16.800 --> 07:23.190
Uh, given the case that you need to handle which of those loops to use.

07:23.190 --> 07:27.570
So I wouldn't go with any generic recommendations.

07:27.570 --> 07:28.890
There are many options.

07:28.890 --> 07:32.520
So we would be just choosing the best options.

07:32.520 --> 07:36.000
That just feels more natural for the job.

07:36.000 --> 07:45.270
But eventually to actually have this instincts of which loops you should be choosing, well, you just

07:45.270 --> 07:47.610
need to write a lot of code.
