WEBVTT

00:01.400 --> 00:03.480
Okay, now you know how to use condition.

00:03.600 --> 00:07.240
Let's go, uh, learn loops.

00:07.360 --> 00:09.520
Okay, let's create a file.

00:09.680 --> 00:10.280
Loops.

00:10.280 --> 00:11.040
Dot py.

00:12.080 --> 00:15.840
And Python has two primitive loops.

00:15.840 --> 00:16.480
Command.

00:16.520 --> 00:16.920
Okay.

00:17.760 --> 00:19.360
One of them is while.

00:19.880 --> 00:21.520
Let me write that here while.

00:21.520 --> 00:23.320
And the next one is for.

00:24.120 --> 00:28.520
So we used a for loop before.

00:28.520 --> 00:35.680
And you know a little thing about for right now we are going to learn while loop now and for loop in

00:35.720 --> 00:37.040
the next lecture.

00:37.560 --> 00:46.360
So while loop with the while loop we can execute a set of statements as long as a condition is true.

00:47.120 --> 00:47.440
How?

00:47.800 --> 00:50.320
Let me give you a simple example here.

00:50.480 --> 00:51.120
Okay.

00:51.160 --> 00:56.640
Just imagine x or I is equal to a number which is one okay.

00:57.120 --> 01:02.240
Now let's use while here give a condition okay.

01:02.880 --> 01:12.260
For example right now I am printing uh this I as long as I is less than a number, for example ten.

01:12.300 --> 01:15.420
Okay, so I say I less than.

01:15.420 --> 01:17.540
Now you know how to use less than okay.

01:17.860 --> 01:19.900
Less than a number which is ten.

01:20.020 --> 01:21.020
Okay.

01:21.020 --> 01:25.220
In here let's print an I.

01:25.220 --> 01:32.100
And then in the next statement in the next line you need to increment I by one.

01:32.140 --> 01:32.820
Okay.

01:32.860 --> 01:34.220
So how can I do that.

01:34.300 --> 01:38.980
Very easy I say I plus equal to one.

01:39.140 --> 01:40.300
What is this mean?

01:40.460 --> 01:47.500
This means each time that this one is repeated, increment this I by one.

01:47.500 --> 01:48.180
Okay.

01:48.220 --> 01:52.180
Or we can do this in another way as well.

01:52.460 --> 01:55.300
I can say I plus.

01:57.340 --> 01:57.780
One.

01:57.820 --> 01:58.340
Okay.

01:58.380 --> 01:59.940
This is one way that we can do.

02:00.340 --> 02:05.460
And also this one is another way.

02:06.020 --> 02:08.660
Let's save it and run this program.

02:12.820 --> 02:19.160
Now you see it is giving me from one app to Two and nine.

02:19.200 --> 02:19.640
Okay.

02:20.160 --> 02:26.720
And here it's not giving me ten, because I am telling the condition that it needs to be less than ten.

02:26.760 --> 02:31.880
If I use less or equal, then it will give me number ten as well.

02:32.080 --> 02:37.480
So that is how you can use and how you can use while loop.

02:37.920 --> 02:44.920
And also we can use another statement that is called break statement.

02:44.920 --> 02:50.760
With the break statement we can stop the loop even if the while condition is true.

02:51.440 --> 02:53.400
So how.

02:53.400 --> 02:55.440
Let's go and see that as well.

02:55.920 --> 03:02.200
Right here inside this I can use an if condition before we learn something about if condition.

03:02.240 --> 03:05.280
Now you know what is that mean okay.

03:05.280 --> 03:12.640
If I is equal to three for example then let's break.

03:12.800 --> 03:13.600
Okay.

03:13.640 --> 03:20.520
While we are using if condition here, we need to give colon and be careful about this space okay.

03:20.560 --> 03:23.800
And right here let's use break statement.

03:23.960 --> 03:27.260
And now save this Execute this.

03:27.700 --> 03:31.380
And you see we have only three number here.

03:31.660 --> 03:31.980
Why?

03:32.020 --> 03:35.300
Because I used break statement right here.

03:35.460 --> 03:41.780
And also there is another statement that we can use with while loop that is called continue with the

03:41.780 --> 03:43.180
continue statement.

03:43.220 --> 03:48.020
We can stop the current iteration and continue with the next one.

03:48.380 --> 03:48.980
Okay.

03:49.020 --> 03:56.620
Right here instead of break right now if I use continue it will do something.

03:56.620 --> 03:57.220
Let's see.

03:57.940 --> 04:02.660
Continue and save this now.

04:03.900 --> 04:05.820
Actually not right in here.

04:07.460 --> 04:11.500
Let me remove this part and go from the beginning.

04:11.540 --> 04:12.820
Okay.

04:12.860 --> 04:13.940
Hit enter.

04:13.980 --> 04:19.220
Now let's say I is going to be incremented by one.

04:19.340 --> 04:27.300
And if this I is equal while we use two, equal means equality okay.

04:27.340 --> 04:30.300
But if I use one equal that means assigning.

04:30.620 --> 04:35.280
So if it is equal to three then let's say continue New.

04:35.280 --> 04:41.760
And here let's print this number which is I right now.

04:41.840 --> 04:43.960
It will start iterating okay.

04:44.000 --> 04:44.720
True.

04:44.720 --> 04:47.240
And checking for the if condition.

04:47.240 --> 04:50.200
And if this I is equal to three.

04:50.200 --> 04:56.920
So it will not print it and go for the next uh condition.

04:56.920 --> 04:57.440
Let's see.

04:58.880 --> 05:03.480
Right now we have one and we don't have one okay.

05:04.040 --> 05:06.400
We have two and then we have four.

05:06.800 --> 05:11.440
Let me change it to another number for example seven okay.

05:12.800 --> 05:14.120
Now let's see that.

05:16.600 --> 05:17.400
Okay.

05:17.440 --> 05:20.360
See we have 23456.

05:20.520 --> 05:23.040
And we don't have seven right in here okay.

05:23.080 --> 05:24.360
And then we have it.

05:24.360 --> 05:30.800
That is because of this continue keyword or continue statement.

05:31.440 --> 05:33.880
As you see here we use if condition.

05:33.880 --> 05:37.560
And also we can use else condition as well.

05:38.400 --> 05:40.000
And that is very easy okay.

05:40.000 --> 05:41.160
You know how to use it.

05:41.280 --> 05:44.280
So thanks for watching and I will see you in the next lecture.
