WEBVTT

00:00.050 --> 00:05.930
Next up, let's talk about the control structures, starting with the if statement.

00:05.960 --> 00:12.680
So as the name suggests, the control structures are used to control the flow of the program.

00:12.680 --> 00:21.740
And in the case of the if statement, you just conditionally run some statements if the expression that

00:21.740 --> 00:24.440
is being evaluated is true.

00:24.470 --> 00:33.920
Now you can also provide alternative cases using else if you can check for alternative conditions.

00:33.920 --> 00:43.820
If this first condition was false, and you can add as many as those alternative paths as you need,

00:43.820 --> 00:52.670
and only one of those will run and they will be checked until there is no other alternative condition.

00:52.670 --> 01:01.870
And optionally you can also add an else statement which will be run if none of those above conditions

01:01.870 --> 01:03.760
evaluated to true.

01:04.360 --> 01:10.210
As a quick reminder, the expression is something that always returns a value.

01:10.360 --> 01:14.800
That's what differentiates the expression from statement.

01:14.830 --> 01:21.850
Statement can be an expression, but it can also be a control structure like the if statement, which

01:21.880 --> 01:26.080
in this case means that if statements can be nested.

01:26.110 --> 01:30.070
Okay, let's do some practical exercises now.

01:30.760 --> 01:31.000
Okay.

01:31.030 --> 01:35.560
So let's jump into the code editor and create a new file.

01:35.590 --> 01:41.470
I'm going to call it FP as that's the topic of this video.

01:41.500 --> 01:43.090
Let me add a PHP tag.

01:43.090 --> 01:46.930
And immediately let's start with the first example.

01:46.930 --> 01:50.290
So for that I'm going to just declare a variable.

01:50.410 --> 01:53.560
Let's assign the value of ten to it.

01:53.560 --> 01:56.710
And let's write a simple if statement.

01:56.710 --> 02:03.240
So it starts with the if keyword, then we've got parentheses that's mandatory.

02:03.240 --> 02:10.020
And inside it we need to add an expression that will evaluate to boolean value.

02:11.370 --> 02:19.500
So the condition inside the parentheses must evaluate to a Boolean value because we essentially have

02:19.530 --> 02:20.910
two choices.

02:20.910 --> 02:29.250
So either this conditional statement that depends on this condition inside parentheses will run or it

02:29.250 --> 02:29.880
won't.

02:30.090 --> 02:32.910
And that's why it needs to be a boolean.

02:32.940 --> 02:35.490
Now I'm adding a curly braces.

02:35.490 --> 02:37.980
So first let me add a condition here.

02:37.980 --> 02:42.750
I'm gonna just check if x is bigger than five.

02:42.750 --> 02:46.830
So currently I think we can be sure that that's true.

02:46.830 --> 02:49.470
But that's the thing with variables.

02:49.470 --> 02:51.510
Variables can change.

02:51.510 --> 02:55.380
And there might be a different value here that needs to be verified.

02:55.410 --> 03:01.400
Now I'm just going to say x is greater than five.

03:01.400 --> 03:06.140
And let me add a new line here and let's run it.

03:06.170 --> 03:07.280
That's BHP.

03:07.310 --> 03:08.270
If BHP.

03:08.300 --> 03:12.980
Now obviously in this case ten is greater than five.

03:13.250 --> 03:21.380
Most people prefer this convention of always using curly braces, but they are not always mandatory.

03:21.380 --> 03:30.440
So if you would only have one statement inside your if control structure, then you can skip the curly

03:30.440 --> 03:30.980
braces.

03:30.980 --> 03:32.660
So this is also fine.

03:32.660 --> 03:35.540
And I can confirm that by running this code.

03:36.470 --> 03:39.230
So I'm gonna use the curly braces everywhere.

03:39.230 --> 03:42.080
I think it is a good convention.

03:42.080 --> 03:48.590
It makes the code much more clear, especially if you have nested if statements.

03:49.730 --> 03:55.040
So this statement only runs if this condition is true.

03:55.040 --> 03:58.960
But if this condition is false, then nothing happens.

03:58.960 --> 04:03.610
So let's see another example where we're going to have some alternatives.

04:03.610 --> 04:10.540
And this is calculation of the grade for a student based on his score in points.

04:10.540 --> 04:15.610
So let's say the score is 85 out of 100 points possible.

04:15.610 --> 04:18.370
Now we need to give students the grade.

04:18.370 --> 04:21.070
So we check for the score.

04:21.100 --> 04:26.620
If it's more than 90 we're going to just echo a.

04:26.650 --> 04:30.190
Keep in mind this here is not one character.

04:30.220 --> 04:36.370
Those are actually two characters more than followed immediately by equality sign.

04:36.370 --> 04:38.860
It's just called a ligature.

04:38.860 --> 04:45.010
And this is how the text editor just renders that those are two characters.

04:45.100 --> 04:45.640
Okay.

04:45.670 --> 04:53.410
So now I want to provide an alternative, because we can expect that it's quite possible that a lot

04:53.440 --> 04:57.210
of people will have a score lower than 90.

04:57.210 --> 05:05.490
So in this case, I'm gonna check if maybe the score is more than or equal 80.

05:05.520 --> 05:09.270
And then I'm going to give it a B.

05:09.420 --> 05:13.560
So let me copy paste that two more times.

05:14.580 --> 05:19.590
So we've got different thresholds in here 7060.

05:19.710 --> 05:23.850
This results in C and this results in D.

05:24.030 --> 05:32.670
Now for all other cases when someone is below 60 there is no point in actually adding a specific condition

05:32.670 --> 05:33.450
for this.

05:33.450 --> 05:37.200
And in that cases we can just go with else.

05:37.200 --> 05:43.590
So for all the other cases then you're getting an F.

05:45.240 --> 05:51.540
So this might also cover a case when someone doesn't have a score, because maybe he just didn't show

05:51.540 --> 05:52.890
up for the exam.

05:52.890 --> 05:58.190
And then he gets F Okay, so now let's run it.

05:58.220 --> 06:09.920
We do PHP if PHP and in this case the score is B, if I change the score to, let's say 60 to be on

06:09.920 --> 06:13.190
the edge, this is D.

06:14.570 --> 06:14.870
Okay.

06:14.870 --> 06:17.060
So that was another example.

06:17.060 --> 06:21.710
Now let's see how can we use nested if statements.

06:24.050 --> 06:30.320
So next up let's define a variable that will be just called num as a number.

06:30.320 --> 06:37.190
And we'll try to say something about the number that is currently inside the variable.

06:38.210 --> 06:43.760
So the first condition here is checking if the number is something positive.

06:43.760 --> 06:46.010
So something more than zero.

06:47.600 --> 06:56.830
And if that's not the case we just echo non A positive number.

06:58.930 --> 07:04.630
But if it is positive, we're going to check if this is an even or an odd number.

07:06.010 --> 07:13.750
Now, the easy way to check if the number is even or odd is to use the modulo operator, which just

07:13.750 --> 07:18.730
gives you a remainder of division by two.

07:18.760 --> 07:25.990
So if in this case that is zero, then we know we have an even number.

07:26.830 --> 07:31.420
So we can say a positive even number.

07:32.470 --> 07:42.280
And in the other case we just used else and we say echo positive odd number.

07:42.940 --> 07:50.410
Now let me add a new line character before every single one of those strings and after.

07:50.410 --> 07:53.820
So that we just have a clear output.

07:53.820 --> 07:57.420
And now let's run this if script.

07:57.420 --> 08:01.620
So currently we've got a positive odd number.

08:02.190 --> 08:03.630
That's the case.

08:03.720 --> 08:07.890
Now let's rerun it again by changing the number to 14.

08:07.920 --> 08:10.020
That's a positive even number.

08:10.050 --> 08:11.340
Yes that's correct.

08:11.340 --> 08:17.010
And minus three this is a non positive number.

08:18.510 --> 08:25.740
You should know that in control structures which just have one statement inside their body.

08:25.740 --> 08:30.810
And the body is basically everything between the curly braces.

08:30.810 --> 08:31.800
So that's a body.

08:31.800 --> 08:37.860
For example if there is only one statement inside you don't need to use curly braces.

08:37.860 --> 08:44.970
So now I can remove all of those curly braces here and everything would still work fine.

08:45.810 --> 08:54.740
Now most people would prefer the convention to always add curly braces, But just so you know, it is

08:54.740 --> 08:55.880
just a convention.

08:55.880 --> 08:59.240
And this code here is totally fine.

08:59.240 --> 09:05.480
And it would work and I can run it and everything is fine.

09:05.480 --> 09:12.470
So just so you know, this nested if statement is basically considered bank one statement.

09:12.470 --> 09:16.310
So I'm going to bring it back to using curly braces.

09:17.480 --> 09:21.590
But well you should know about all the possibilities.

09:21.590 --> 09:30.050
And another benefit of using the curly braces is that I get this highlighting in the code editor of

09:30.050 --> 09:31.760
the matching curly braces.

09:31.760 --> 09:39.290
This really helps if you have bigger blocks of code that might be longer, and might really be longer

09:39.290 --> 09:42.320
than what you currently see on the screen.

09:43.520 --> 09:49.250
Okay, so let's have a final example with the username and password.

09:49.250 --> 09:51.170
So let's define two variables.

09:51.170 --> 09:59.440
The username, which would be admin, and the password which can be just password one two, three.

09:59.680 --> 10:08.860
Now we're gonna add an if statement and we'll check if the username is specifically admin.

10:09.430 --> 10:16.840
And we're going to add a logical operator because we also need to make sure that both are true.

10:16.840 --> 10:23.020
So username is exactly admin and that also password.

10:23.050 --> 10:24.190
That's why we use this.

10:24.220 --> 10:30.610
And operator that password is exactly password 123.

10:33.250 --> 10:37.450
So if that's fine we just say success.

10:37.450 --> 10:41.140
And otherwise you can say failure.

10:41.530 --> 10:43.870
I'm missing the echo statement here.

10:43.900 --> 10:52.440
Now just keep in mind that in real applications you will have the users and the passwords stored inside

10:52.440 --> 10:56.850
the database, and they would not be stored as plain text.

10:56.850 --> 10:59.220
They will be encoded.

10:59.250 --> 11:03.060
So this here is just a simple example.

11:03.060 --> 11:06.990
So we can showcase more examples with the if statements.

11:07.020 --> 11:09.810
Now let's run that in the terminal.

11:09.810 --> 11:11.580
And we have a success.

11:11.580 --> 11:18.750
And if I would change this password even slightly we should have a failure.

11:18.870 --> 11:19.470
Okay.

11:19.500 --> 11:21.030
So let's summarize.

11:21.030 --> 11:26.640
So we've seen some new operators like this logical operator.

11:26.640 --> 11:36.540
We've had examples of the if statements by having the else alternatives and also else if alternatives.

11:36.540 --> 11:39.960
We've used the nested if statements.

11:39.960 --> 11:47.730
And also we learned that the control structures, if there is just one statement inside their body,

11:47.760 --> 11:50.250
you can skip the curly braces.
