WEBVTT 00:00.310 --> 00:04.140 It you guys will come back in this lecture we'll be talking about loops. 00:04.260 --> 00:10.030 So if know have any task that has quite repetitive and has to be done every time. 00:10.080 --> 00:16.300 Or is there any conditions which keep changing and need to be checked after every certain block of. 00:16.570 --> 00:18.510 Then we will start using Lu. 00:18.660 --> 00:21.330 So in this video we'll be talking about why. 00:21.660 --> 00:26.870 So in the previous example or in the previous lecture we talked about if an else condition. 00:27.030 --> 00:32.520 So when we are checking any condition and if the condition is true then you are proceeding for a certain 00:32.520 --> 00:38.420 block of all that is brain statement or it can be anything that is inside this inundation. 00:38.580 --> 00:42.510 And if the condition is false we are moving towards the end scrimmaging. 00:42.630 --> 00:48.630 But what if I need to reject this condition off a certain block of all or if the variable we are using 00:48.630 --> 00:49.870 keeps changing. 00:50.040 --> 00:52.320 So for that we start using loops. 00:52.410 --> 00:58.920 But before thinking anything is this clean up your own mind and take the basic example of a new something 00:58.920 --> 01:00.810 that you need to look at them. 01:00.870 --> 01:08.210 You try to use a certain block of or and you just run the amount that I need to do this for 10 times. 01:08.250 --> 01:11.000 So for that particular thing loop is used. 01:11.160 --> 01:17.940 This is the most basic common example if I want to print 1 200 use or lose that print one then print 01:17.940 --> 01:21.510 to print reprint Faulkland fine print 6 and so on. 01:21.630 --> 01:24.710 So this is the most common and basic example. 01:24.930 --> 01:30.110 Now before talking more let us start with why new and Barhoum a simple example. 01:30.420 --> 01:33.730 So herem taking a variable x as 0. 01:33.780 --> 01:37.100 And my problem is I need to print from 1 to 5. 01:37.320 --> 01:39.380 So for that I'm using Y new. 01:39.570 --> 01:44.930 To start of I know all you have to do is type 1 and then you have to give up a notion. 01:45.170 --> 01:49.550 So for that I'm using x distance x I will be explaining why. 01:49.680 --> 01:56.510 But for a moment let's take this and as the condition ends use school and then you will get into the 01:56.620 --> 02:00.460 inundation that is the block and now you have to start printing. 02:00.510 --> 02:03.130 So our problem is tripling from zero to 5. 02:03.300 --> 02:09.450 All I need to do is bring the current value of x which is zero. 02:09.460 --> 02:16.750 Now I just want to find Xs you know what I can do is I can add 1 2 x then 0 plus 1 will be 1. 02:16.860 --> 02:23.580 And once this loop and it will jump back to this condition and check if X is less than 6 if the condition 02:23.580 --> 02:29.250 is true it would jump again inside this particular block of all and pretend that the current value of 02:29.310 --> 02:30.340 x is 1. 02:30.510 --> 02:37.980 It will come on this condition again and add one again then X will be to jump back to this condition 02:38.200 --> 02:45.210 range x is equal to two and one again that will make X's three jump back to this condition. 02:45.270 --> 02:52.950 And once the condition of X is 6 This will give false blue white blue and then it will get out of the 02:52.950 --> 02:53.380 hole. 02:53.430 --> 02:56.570 So for now let us exclude this and see our result. 02:57.590 --> 03:04.190 You can see it's not correctly printing now value of x 5 you can say. 03:04.520 --> 03:11.280 So the basic thing is in my IF statement if the condition is true it will only do the task for once. 03:11.330 --> 03:17.570 But in loops if the condition is true it will execute executed and then go back to check out the condition 03:17.990 --> 03:23.020 and never remove this particular line of or to increase the value of x. 03:23.090 --> 03:25.010 X will remain zero forever. 03:25.070 --> 03:27.420 That means X is less than zero. 03:27.470 --> 03:29.520 It will bring the value of x. 03:29.540 --> 03:31.570 Then go back to this particular condition. 03:31.670 --> 03:35.750 Still it's a zero radius this value to this condition. 03:35.870 --> 03:37.820 Still the value of x is zero. 03:37.880 --> 03:38.860 In this condition. 03:39.000 --> 03:44.950 So this loop is going to be forever and it will keep on printing this particular statement whatever. 03:45.110 --> 03:52.970 If you want to try I can show you and remember if you want to disturb the infinite blue all you have 03:52.970 --> 03:54.340 been loses control. 03:54.350 --> 04:01.700 Now see that means and are dominant whenever we are running our nation and it's infinite blue or whenever 04:01.700 --> 04:05.510 you want to break any condition break any code you can use control. 04:05.570 --> 04:06.100 See. 04:06.140 --> 04:08.260 Let me try out. 04:08.420 --> 04:14.570 You can see it's keep on printing and let me break this condition and let me show you my thumb and you 04:14.570 --> 04:15.680 can see it. 04:15.680 --> 04:17.540 So anytime I am scrolling up 04:23.840 --> 04:26.210 you can see that you get an infinite loop. 04:26.210 --> 04:33.410 So this is the reason that we need to be careful when using any two because then you can get into repetitive 04:33.410 --> 04:36.610 task and that task can run for infinite pain. 04:36.820 --> 04:41.980 So let me get back my condition and what I can do is I can use X plus. 04:42.000 --> 04:45.590 And then when that was the thing. 04:45.590 --> 04:52.410 Now one more important thing as you are using it is you can also use as well. 04:52.490 --> 04:54.400 So this is conditional. 04:54.530 --> 04:57.790 When this particular condition is false. 04:57.920 --> 05:04.790 So here I can add something that would be executed once the new mission is completed. 05:05.150 --> 05:13.270 Now let me run this come on say you can see all the conditions of why you executed and then we have 05:13.270 --> 05:18.930 a statement of continued why the because still five this condition was true. 05:19.000 --> 05:25.960 And once the value of X was six this condition gets into false statement that means we'll move towards 05:25.960 --> 05:27.390 the statement. 05:27.460 --> 05:29.470 And this condition will be. 05:29.890 --> 05:32.530 So one thing you need to remember is if we are using. 05:32.540 --> 05:40.590 Else why Lou there is commission will be executed always one stop particular condition of mine who is 05:40.600 --> 05:46.840 trying to improve one's knowledge has become one more example with wildblue which will help me to understand 05:46.840 --> 05:54.160 this in a long time because while loop is used to bring back down swine flu do we use when you try to 05:54.160 --> 05:56.630 check condition or exit any screen. 05:56.710 --> 05:58.680 So we're taking that example. 05:58.820 --> 06:07.090 When my task is to keep taking them from user and print to some and once the user enters zero that means 06:07.090 --> 06:14.140 my program is to select let me take two variables one Islam and taking its value as one. 06:14.140 --> 06:16.270 I will explain you in a minute. 06:16.480 --> 06:22.970 And the second really while I NEED SOME which is you know now I need to provide instruction to the user 06:23.090 --> 06:24.930 of that revenue. 06:25.070 --> 06:31.290 And if you want to accept this program press zero so we can use print statement to provide instruction. 06:31.630 --> 06:35.020 And once that is done I need to use a Y Y. 06:35.050 --> 06:41.110 So whenever the value of numbers 0 that means the condition is false and our Y loop is stockbroking 06:41.200 --> 06:43.720 and we will be entering into this condition. 06:43.750 --> 06:47.830 So I'm using our condition as NUMP is not equal to zero. 06:47.950 --> 06:50.930 That means keep adding that number into song. 06:51.160 --> 06:53.540 So let me take my loop. 06:53.870 --> 06:57.470 So now is not equal to zero that means this condition is true. 06:57.550 --> 07:03.270 And then I can get inside the block and if the number 0 that means keeping the ends. 07:03.430 --> 07:09.640 And now I need big input from the user and this input will be in strings so I can either convert it 07:09.640 --> 07:16.110 into slow or integer according to your requirement for integer. 07:16.110 --> 07:19.930 Now at this point of time I have taken the input from the user. 07:19.950 --> 07:21.570 Now I need to add them. 07:21.700 --> 07:26.330 So currently all the values that I'm getting from user I will be adding that to the sum. 07:26.520 --> 07:28.700 I just wanted to find some in 0. 07:28.710 --> 07:37.620 Now I need to add the value that has taken from the user to some Belviq was to some last number. 07:37.770 --> 07:39.000 And now I can print the sum. 07:39.000 --> 07:45.200 So every time I'm going inside the loop I will be printing this on OK. 07:45.310 --> 07:48.220 So I just want to find my mission is score. 07:48.370 --> 07:50.140 So let me write a statement. 07:50.140 --> 07:55.560 If you don't want to write you can skip that Mike I'm just writing for a loop completion. 07:55.700 --> 08:02.320 No record what is happening is I've taken a very well known as one you can assign a zero. 08:02.590 --> 08:07.800 So what we need is when you predict an input from a user and keep doing this. 08:07.960 --> 08:14.020 Now doited the instruction to the user that if they are going to press 0 then the program will stop 08:14.420 --> 08:17.440 and that we need to keep our number. 08:17.440 --> 08:22.990 Now I have taken a while to check the condition that number is not equal to zero. 08:22.990 --> 08:25.840 We are going to run this will once. 08:25.860 --> 08:28.380 None that the user enters zero. 08:28.570 --> 08:35.680 That means it will be the sum that will not affect 0 0 and or the sum that is the current sum and go 08:35.680 --> 08:37.030 back to the condition. 08:37.030 --> 08:41.080 Now the commission will be false because number is equal to zero. 08:41.200 --> 08:46.770 So this condition is not followed it will jump back to its condition and is completed. 08:46.900 --> 08:54.700 Now let us come on Howard is asking us to enter a number of let me enter then I can in some extend enter 08:54.820 --> 08:59.480 15 I can and some is 25 lignin to zero. 08:59.640 --> 09:03.780 So I can tell some is 25 I completed. 09:03.970 --> 09:09.100 I hope you understand why you can do a lot of things with loops in the next lecture when we're talking 09:09.100 --> 09:10.400 about $400.