1 00:00:08,674 --> 00:00:09,442 Welcome back. 2 00:00:09,442 --> 00:00:14,300 So as we talked about before, a while loop is generally more capable than a for 3 00:00:14,300 --> 00:00:17,756 loop but there are few drawbacks to using while loops. 4 00:00:17,756 --> 00:00:21,658 The first is that it can be a little bit more tedious to express the same concept, 5 00:00:21,658 --> 00:00:25,320 as you might have seen in some of the code samples from before. 6 00:00:25,320 --> 00:00:29,350 But the second is that you can get stuck in what's called an Infinite Loop. 7 00:00:29,350 --> 00:00:32,000 An Infinite Loop is a loop that never terminates. 8 00:00:32,000 --> 00:00:36,140 In other words, your program, if it could, would keep running forever. 9 00:00:36,140 --> 00:00:39,190 Now remember that if we have a while loop like this, 10 00:00:39,190 --> 00:00:41,500 then every while loop has a condition. 11 00:00:46,079 --> 00:00:49,116 And by the time we get to the end of the while loop, 12 00:00:49,116 --> 00:00:54,920 then we're going to go back to the top and check if this condition is still true. 13 00:00:54,920 --> 00:00:59,820 And what that means is that if you ever run the code in this while loop, 14 00:00:59,820 --> 00:01:04,660 then it better have a chance of switching this condition from false to true. 15 00:01:04,660 --> 00:01:08,980 But if that's not the case, in other words, if this condition is always true 16 00:01:10,560 --> 00:01:13,650 and we always reach the end of this while loop, 17 00:01:13,650 --> 00:01:17,530 then our code is going to be stuck in what's called an Infinite Loop. 18 00:01:17,530 --> 00:01:20,050 Because if we always reach the end of this while loop, 19 00:01:20,050 --> 00:01:22,240 then we're always going to go back to the top. 20 00:01:22,240 --> 00:01:23,910 And if this condition is always true, 21 00:01:23,910 --> 00:01:26,840 then we're always going to run this code once more. 22 00:01:26,840 --> 00:01:29,550 And that's called an Infinite Loop because your program would 23 00:01:29,550 --> 00:01:32,106 keep running infinitely if it could. 24 00:01:32,106 --> 00:01:35,030 In reality of this textbook has a mechanism to 25 00:01:35,030 --> 00:01:37,300 prevent your code from running too long and 26 00:01:37,300 --> 00:01:41,510 pretty much every interpreter has some way of breaking out of an Infinite Loop. 27 00:01:41,510 --> 00:01:43,760 But they can be frustrating, nonetheless, and 28 00:01:43,760 --> 00:01:45,250 they're important to be able to recognize. 29 00:01:46,540 --> 00:01:50,400 Now this code is actually stuck in an Infinite Loop. 30 00:01:50,400 --> 00:01:55,410 So if I ran my code, now I would see first that my cursor 31 00:01:55,410 --> 00:01:59,040 is stuck on a pointer, so that tells me that is something is wrong. 32 00:01:59,040 --> 00:02:03,620 If I even try to click or type anything 33 00:02:03,620 --> 00:02:06,980 then I am not going to be able to, because this computer is working so 34 00:02:06,980 --> 00:02:11,660 hard trying to run this little while look that has three lines. 35 00:02:11,660 --> 00:02:13,360 Now, after a while then, 36 00:02:13,360 --> 00:02:17,240 the Python interpreter is going to say that this code ran too long but, 37 00:02:17,240 --> 00:02:21,020 until then, I'm not going to be able to do anything on the page. 38 00:02:21,020 --> 00:02:23,380 So, as it's evaluating this, 39 00:02:23,380 --> 00:02:27,330 let's try to analyze why we're actually stuck in an Infinite Loop. 40 00:02:27,330 --> 00:02:28,740 So, here we go. 41 00:02:28,740 --> 00:02:30,730 The code finally finished running and 42 00:02:30,730 --> 00:02:35,730 you can see that it printed out Bugs many, many times. 43 00:02:35,730 --> 00:02:37,232 So, I'm going to scroll to the bottom here. 44 00:02:37,232 --> 00:02:42,230 You can see that it printed out Bugs a lot. 45 00:02:42,230 --> 00:02:43,570 And at the end, 46 00:02:43,570 --> 00:02:47,940 I'm going to get a message that this program exceeded the run time limit. 47 00:02:47,940 --> 00:02:51,790 So in other words, this program tried to run for too long and 48 00:02:52,870 --> 00:02:56,930 this probably indicates that your program is stuck in an Infinite Loop. 49 00:02:56,930 --> 00:02:59,690 So let's look at why this program is stuck in an Infinite Loop. 50 00:03:01,410 --> 00:03:04,929 So here we initialize the variable b to be 15. 51 00:03:04,929 --> 00:03:09,990 So that looks good so far and we say while b is less than 60. 52 00:03:09,990 --> 00:03:14,480 Okay, so this condition is of course going to be true while b is 15. 53 00:03:14,480 --> 00:03:17,360 So when b is 15, then we're going to run this code. 54 00:03:18,550 --> 00:03:21,065 And then we say b = 5. 55 00:03:21,065 --> 00:03:22,785 Hm, well, 56 00:03:22,785 --> 00:03:28,330 that looks a little bit out of place, but let's come back to it in a little bit. 57 00:03:28,330 --> 00:03:30,164 On line 5, we print out Bugs, 58 00:03:30,164 --> 00:03:34,761 which as you can see gets printed out a lot because this while loop is run a lot. 59 00:03:34,761 --> 00:03:39,575 And then on line 6 we say b equals its previous value plus 7. 60 00:03:40,620 --> 00:03:44,960 Now, let's come back to this line b = 5 because 61 00:03:44,960 --> 00:03:50,280 what assigning b = 5 does here is it kind of resets the value of b. 62 00:03:50,280 --> 00:03:54,980 So again our check is if b is less than 60. 63 00:03:54,980 --> 00:04:00,620 And so if we keep resetting b to be 5 even if we increment its value 64 00:04:00,620 --> 00:04:07,306 to be 12 later on by adding 7 to it then b is still always going to be less than 65 00:04:07,306 --> 00:04:13,062 60 because every time we run this loop we reset b's value to 5. 66 00:04:13,062 --> 00:04:17,380 So maybe if line 4 was accidental, so if we commented it out, 67 00:04:17,380 --> 00:04:20,628 then we would actually be able to run our code. 68 00:04:23,952 --> 00:04:24,681 So again, 69 00:04:24,681 --> 00:04:30,030 the problem here is that we were resetting b to be 5 every time we ran this code. 70 00:04:31,380 --> 00:04:36,010 So, it's important to be able to recognize how Infinite Loops occur. 71 00:04:36,010 --> 00:04:38,420 They can occur in many, many ways. 72 00:04:38,420 --> 00:04:43,730 So, this example is just one of many ways to accidentally write an Infinite Loop. 73 00:04:43,730 --> 00:04:46,090 As you get more experience in writing while loops, 74 00:04:46,090 --> 00:04:50,820 you'll become more adept at being able to identify and fix Infinite Loops. 75 00:04:50,820 --> 00:04:52,268 That's all for now until next time.