WEBVTT

00:00.380 --> 00:03.830
Let me show you another variant of the while loop.

00:03.830 --> 00:07.100
This time it can be called do while and.

00:07.100 --> 00:14.120
The difference between this while loop that we've seen previously is that in the case of the do while

00:14.120 --> 00:24.140
loop, the statements, the loop body is always executed at least once and only repeated if the expression

00:24.140 --> 00:25.340
returns true.

00:25.370 --> 00:28.220
So that's the main difference with the while loop.

00:28.220 --> 00:34.130
The expression needs to be true first for the body to run with the do while loop.

00:34.160 --> 00:39.050
The body runs first once and is repeated.

00:39.050 --> 00:44.030
If this condition, the expression in the condition returns true.

00:44.180 --> 00:46.130
Let's see a fun example.

00:47.480 --> 00:49.760
So first let's create a new file.

00:49.760 --> 00:56.990
Let's call it do while PHP and open it immediately add a PHP tag.

00:57.020 --> 01:01.850
So as an example we're going to create a simple dice roll game.

01:02.210 --> 01:04.430
So let's begin with the syntax.

01:04.430 --> 01:09.860
It starts with this do keyword followed by the curly braces.

01:09.860 --> 01:13.820
And that's the loop body.

01:14.360 --> 01:19.700
And the condition goes after this last curly brace.

01:19.700 --> 01:29.570
And only then you write while and here goes the condition the expression okay so dice roll game.

01:30.530 --> 01:33.290
We need to pick a random number.

01:33.890 --> 01:38.990
So let me define a dice roll variable inside.

01:39.890 --> 01:46.160
So again I'm going to use a function I know I haven't explained that yet, but without using some of

01:46.160 --> 01:51.530
the things explained in the future, those examples would be pretty dull.

01:51.530 --> 01:53.840
So you just need to bear with me.

01:53.840 --> 02:00.980
This code right here basically picks a random number between 1 and 6.

02:01.340 --> 02:02.240
Okay.

02:02.360 --> 02:06.530
Um, so we need to tell the user what's his role.

02:06.530 --> 02:17.180
So you rolled a and we can also immediately just output this variable inside double quotes.

02:18.140 --> 02:19.910
Add a new line character.

02:20.540 --> 02:20.990
Okay.

02:20.990 --> 02:23.930
So at this point we know what's our role.

02:23.960 --> 02:26.360
If someone rolls six.

02:26.810 --> 02:34.460
Remember about using the value on the left side of comparison to be super safe.

02:37.400 --> 02:38.270
Like that.

02:38.300 --> 02:47.810
We say congrats, you hit the jackpot.

02:48.920 --> 02:54.020
Okay, new line and let's add some celebration icon.

02:57.050 --> 02:58.370
Maybe this.

02:58.730 --> 02:59.660
All right.

02:59.690 --> 03:00.980
Semicolon.

03:00.980 --> 03:04.070
And what's next?

03:04.370 --> 03:09.560
Next we need to ask if the guy wants to roll again.

03:09.860 --> 03:11.390
So we ask a question.

03:11.390 --> 03:12.560
Roll again.

03:12.560 --> 03:16.070
And the answers we accept is yes or no.

03:16.070 --> 03:19.040
So it needs to be lowercase.

03:19.070 --> 03:23.630
And then let's add another variable.

03:23.630 --> 03:27.350
And again here we're going to get the input from the user.

03:27.350 --> 03:29.900
So you've seen that in previous examples.

03:29.900 --> 03:34.970
So there are some function calls like trim to remove the white spaces.

03:34.970 --> 03:41.120
And f gets with SD in to just accept some console input.

03:41.210 --> 03:43.910
And now the condition.

03:43.910 --> 03:47.750
So we only want this to run again if.

03:50.090 --> 03:56.390
Y equals roll again Let's go through this loop again.

03:56.390 --> 03:57.170
The code.

03:57.170 --> 04:04.640
The loop body will always run at least once, so we roll.

04:04.670 --> 04:06.830
We display the result.

04:06.980 --> 04:13.580
We say if someone hit a jackpot and we ask if he wants to roll again.

04:14.300 --> 04:15.830
Now to this roll again.

04:15.860 --> 04:24.200
We just take the user input from the command line and if he typed y then we let him roll again.

04:24.350 --> 04:32.240
So this body will run again, and the guy will be asked for the subsequent rolls until he quits.

04:32.750 --> 04:36.170
So let's save the changes and let's run it.

04:36.200 --> 04:37.460
So this is do while.

04:37.670 --> 04:38.090
PHP.

04:38.390 --> 04:40.220
Okay, so I rolled one.

04:40.760 --> 04:42.050
Let's try again.

04:42.920 --> 04:44.120
There is the jackpot.

04:44.120 --> 04:51.320
And if I choose no or basically any other character, to be honest, the game just quits.
