WEBVTT

00:00.080 --> 00:04.310
Now let's learn the first loop, which is the while loop.

00:04.310 --> 00:11.420
So the loops are different from the if statement in the way that with the if statement.

00:11.420 --> 00:19.640
If the expression in the condition is true, the statements in the body are only run once.

00:19.670 --> 00:25.910
Now, with loops like the while loop, the statements will be run.

00:25.910 --> 00:34.520
As long as this condition is true, this might be infinite number of times.

00:34.520 --> 00:42.740
So that's also something you need to watch out for that you might just create an infinite loop.

00:42.770 --> 00:47.390
Okay, so let's see a practical example of a while loop.

00:47.450 --> 00:53.120
So back in the code editor let's create an example with the while loop.

00:53.120 --> 01:01.610
Now I would like this to be something interesting, as I don't like to learn with boring examples myself.

01:01.610 --> 01:09.820
So let's create a simple guessing game That would run in the command line, and the job of the user

01:09.820 --> 01:12.640
would be to guess our password.

01:13.030 --> 01:18.970
So we need a variable where we're going to keep this password.

01:18.970 --> 01:21.280
Let it be magic.

01:21.310 --> 01:23.080
You can come up with something else.

01:23.080 --> 01:28.690
Then let's add a variable to count the attempts that someone has made.

01:29.380 --> 01:30.730
Start with zero.

01:30.880 --> 01:37.840
Then let's decide on the maximum attempts before his luck is running out.

01:37.870 --> 01:39.250
Let it be five.

01:39.250 --> 01:41.320
And now the game.

01:41.320 --> 01:42.880
So the while loop.

01:42.880 --> 01:44.110
That was pretty simple.

01:44.110 --> 01:52.300
We've got the while keyword parentheses that's mandatory to add them, and the expression that needs

01:52.300 --> 01:54.340
to go inside the condition.

01:54.340 --> 01:56.380
So this would be rather simple.

01:56.380 --> 02:05.770
If the attempts is less than maximum attempts, we run the code that is inside this loop body.

02:06.100 --> 02:08.470
So what could this be?

02:08.470 --> 02:11.420
Well we should first start with a question.

02:11.420 --> 02:16.280
So let's give them a chance to guess the password.

02:16.280 --> 02:18.380
So we echo that on the screen.

02:18.530 --> 02:21.470
So next we need to take the input from the user.

02:21.470 --> 02:23.750
Let's store it in a variable.

02:23.750 --> 02:30.170
And we also need to forget for a second that we haven't learned about the functions yet.

02:30.200 --> 02:36.170
And the only way to accept the input would be to call this specific functions.

02:37.910 --> 02:42.710
So let me just type that and we're going to talk about this later on.

02:42.710 --> 02:51.260
So those two functions basically they take the input from the command line and trim the white spaces

02:51.740 --> 02:52.310
okay.

02:52.310 --> 02:58.700
So at this point we are asking for the password for the user to guess the password.

02:58.700 --> 03:00.110
We take his answer.

03:00.110 --> 03:10.640
And since we use this attempts to figure out if we should still let him try again, we need to increase

03:10.640 --> 03:12.260
the attempts.

03:12.770 --> 03:20.260
So in PHP you have this plus plus operator that basically is increasing an integer by one.

03:20.290 --> 03:22.030
So we need to do that.

03:22.030 --> 03:24.640
Remember about infinite loops.

03:24.640 --> 03:32.140
If we would forget to increase this attempts variable value, we might end up with a loop that just

03:32.140 --> 03:33.790
runs forever.

03:34.630 --> 03:37.510
Okay, so now we need to take some decisions.

03:37.510 --> 03:48.520
Fortunately we know the if statement so we can see if maybe the guess is identical to our secret.

03:49.750 --> 03:53.800
So if that's the case we're just gonna echo.

03:54.400 --> 03:55.510
Correct.

03:57.310 --> 04:02.380
You've unlocked the treasure.

04:04.810 --> 04:12.400
Let's add a new line character, and maybe I'm gonna put some special symbol so it looks nice.

04:13.420 --> 04:16.840
Let me search for a diamond.

04:16.960 --> 04:23.880
You can copy those characters Actors from the code resources that are always on GitHub.

04:23.910 --> 04:26.460
Now we need also an alternative.

04:26.460 --> 04:34.020
And if the attempts are equal, the max attempts.

04:34.020 --> 04:38.760
Well, we need to tell to the user that that's the end of the game.

04:38.940 --> 04:43.170
So let's do echo out of attempts.

04:45.990 --> 04:48.060
The treasure.

04:50.970 --> 04:52.740
Remains locked.

04:55.050 --> 04:56.130
Newline character.

04:56.130 --> 05:00.090
And let's also add a symbol.

05:00.120 --> 05:03.120
Um, maybe just a lock.

05:04.050 --> 05:05.670
I think this would be fine.

05:05.850 --> 05:06.450
Okay.

05:06.480 --> 05:07.560
Semicolon.

05:07.590 --> 05:10.980
Next up, an alternative.

05:10.980 --> 05:13.080
So what can be an alternative?

05:13.110 --> 05:22.260
Either someone has guessed if the guess is different than the secret, then we just check if that was

05:22.260 --> 05:23.400
the last attempt.

05:23.400 --> 05:30.440
But the alternative to this is that wasn't the last attempt and the guess was incorrect.

05:31.040 --> 05:35.030
Which leaves us with saying that someone was wrong.

05:35.030 --> 05:37.430
But he can try again.

05:37.940 --> 05:46.970
And we also should say that there are some attempts left, so attempts left equal.

05:47.000 --> 05:54.830
Now we use the concatenation operator that we already know, and let's close that in parentheses and

05:54.830 --> 06:00.050
subtract the attempts from max attempts.

06:00.050 --> 06:02.600
So we are doing just simple math here.

06:02.600 --> 06:05.480
And let's make sure we add a new line character.

06:07.340 --> 06:11.780
And this is actually the whole game.

06:11.780 --> 06:13.100
One more thing.

06:13.100 --> 06:21.530
So if someone would guess the secret then we actually need to stop this loop, because the way it works

06:21.530 --> 06:30.720
is this loop will run the code that it is inside the body for maximum of five times.

06:30.720 --> 06:32.430
We've got five attempts.

06:32.430 --> 06:39.900
But if we would like to stop this loop earlier because something has happened, like in this case someone

06:39.900 --> 06:45.000
has guessed the password, so there is no point in asking about the password.

06:45.270 --> 06:47.340
Let's say two remaining times.

06:47.370 --> 06:49.740
We can use the break statement.

06:49.740 --> 06:53.100
It will just exit this loop.

06:53.130 --> 06:55.590
Okay, so it's time to try this out.

06:55.590 --> 06:58.290
I think everything should be fine here.

06:58.290 --> 07:00.720
So the password we have it here.

07:00.720 --> 07:03.450
Let me type something that's wrong.

07:03.450 --> 07:04.320
We've got.

07:07.200 --> 07:07.710
Okay.

07:07.710 --> 07:12.540
So we are out of attempts and we see that everything worked fine here.

07:12.540 --> 07:13.560
So let me try again.

07:13.560 --> 07:17.550
But this time I'm gonna type the proper password.

07:17.550 --> 07:18.510
Magic.

07:18.840 --> 07:21.690
We've got the correct success message.

07:21.690 --> 07:24.300
And as you see, the program stopped.

07:24.300 --> 07:32.730
Which also means that this break statement has successfully, well, basically broken us out of the

07:32.730 --> 07:33.360
loop.
