1 00:00:00,150 --> 00:00:07,920 In this tutorial we will talk about the while loop a while loop is used to iterate over a block of code 2 00:00:07,920 --> 00:00:13,380 repeatedly until a given condition returns funds. 3 00:00:13,890 --> 00:00:20,970 And the main difference between the while loop and the for loop the do while loop. 4 00:00:21,060 --> 00:00:32,890 We are not certain of the number of times the loop requires execution on the other hand the default 5 00:00:32,900 --> 00:00:33,520 low. 6 00:00:33,570 --> 00:00:43,490 We know exactly how many times we need to run the loop and this is the syntax for the while loop. 7 00:00:43,840 --> 00:00:48,550 While condition and we have a set of statements in the body. 8 00:00:49,230 --> 00:01:00,270 So here is an example of a while loop the sine five to debatable a and using the while loop. 9 00:01:00,630 --> 00:01:05,390 I'm gonna check this condition if A is greater than zero. 10 00:01:05,540 --> 00:01:14,840 In that case the control goes through into the body or the Y loop but in S E and then the next line 11 00:01:15,170 --> 00:01:19,150 we're going to decrement the value of a by 1. 12 00:01:19,400 --> 00:01:31,510 The while loop get executed until this condition is found the while loop first prints out 5 and then 13 00:01:31,510 --> 00:01:43,450 four three two one that this is the output of the Y loop when the value of A is 0. 14 00:01:43,450 --> 00:01:52,760 Since 0 is none greater than 0 it comes out of the Y loop so the program first checks a given condition 15 00:01:53,090 --> 00:02:00,590 and if the condition returns filed the lupus terminated and the control jumps to the next statement 16 00:02:00,590 --> 00:02:03,110 and the program after the loop. 17 00:02:03,110 --> 00:02:10,340 So in the Jupiter note book I have executed the example that I have just demonstrated and this is the 18 00:02:10,550 --> 00:02:11,560 output. 19 00:02:11,600 --> 00:02:17,210 Next is the infinite loop a loop becomes infinite loop. 20 00:02:17,260 --> 00:02:25,340 If a condition never becomes false and you have to be careful while using a while loop because if you 21 00:02:25,340 --> 00:02:30,710 forget to incremental count the wearable in Python or right. 22 00:02:30,800 --> 00:02:35,810 Flawed logic the condition may never become false. 23 00:02:36,080 --> 00:02:44,120 In that case the loop and run in financially and the conditions after the loop will start to consider 24 00:02:44,120 --> 00:02:47,760 the same example that we have discussed the while loop. 25 00:02:48,260 --> 00:03:00,430 If I remove this particular line in the y loop A will always be five and it will always be greater than 26 00:03:00,430 --> 00:03:01,200 zero. 27 00:03:01,480 --> 00:03:06,580 And the program keeps printing the value of a 28 00:03:10,430 --> 00:03:13,380 so the condition never becomes false. 29 00:03:13,460 --> 00:03:16,940 And this result in a loop that never ends. 30 00:03:17,360 --> 00:03:28,360 So in my JUPITER KNOWN book If I comment on this line and execute this piece of code as you can see 31 00:03:28,360 --> 00:03:41,080 here the program is in an infinite loop to stop execution go to kernel and then choose interrupt not 32 00:03:41,110 --> 00:03:42,790 to stop execution. 33 00:03:42,790 --> 00:03:52,840 Next the else statement for while loop in a previous tutorials we have talked about using the L statement 34 00:03:53,050 --> 00:04:00,290 with the for loop and when we use the L statement with the for loop the L statement is executed when 35 00:04:00,340 --> 00:04:08,110 the for loop has exhausted ie iterating when we use an L statement with a while loop the L statement 36 00:04:08,110 --> 00:04:18,450 is executed then the condition in the while loop becomes false and the L statement is optional and it 37 00:04:18,450 --> 00:04:27,300 get executed if the loop i.e. iteration complete normally but not a word if the for loop or the Y loop 38 00:04:27,540 --> 00:04:38,070 does not end normally such as if we use a break statement or a written statement in that case the control 39 00:04:38,130 --> 00:04:41,220 will not go to the end clause. 40 00:04:41,220 --> 00:04:49,950 Consider this example we have assigned devalue Phi to the variable E and D while loop checks if A is 41 00:04:49,950 --> 00:04:50,850 greater than 0. 42 00:04:51,510 --> 00:05:01,530 If it is greater than 0 it prints out a decrement the value of a by 1 and then continues the loop until 43 00:05:01,650 --> 00:05:04,710 the condition is false. 44 00:05:04,710 --> 00:05:14,830 We have used the L statement with along with the while loop and after only when the while loop complete. 45 00:05:15,240 --> 00:05:22,880 Or when the condition becomes false only then the else clause gets executed. 46 00:05:23,140 --> 00:05:32,370 Let's execute this piece of code so as you can see until the condition is false do while loop word execute 47 00:05:32,370 --> 00:05:38,950 it and then finally the else clause is executed. 48 00:05:38,950 --> 00:05:42,840 Now let's consider then that example using the brake statement. 49 00:05:42,900 --> 00:05:55,560 Consider this piece of code we have used an L statement along with the Y loop the Y loop has a condition 50 00:05:55,920 --> 00:06:00,050 that it checks if the value of a is equal to 2. 51 00:06:00,170 --> 00:06:08,910 In that case it breaks the Y loop so let's execute this piece of code. 52 00:06:10,980 --> 00:06:20,970 If you notice the statement in the else clause is not executed this time that is because the while loop 53 00:06:21,000 --> 00:06:22,310 did not end. 54 00:06:22,590 --> 00:06:29,530 Normally it is terminated prematurely because of the brake statement here. 55 00:06:29,550 --> 00:06:33,930 This is about the L statement with while loop.