1 00:00:00,000 --> 00:00:04,170 While loops are, well they're still a loop, so you can loop through 2 00:00:04,179 --> 00:00:06,720 things. But the difference between a while loop and a for 3 00:00:06,720 --> 00:00:10,300 loop, is that a while loop is going to run forever until 4 00:00:10,300 --> 00:00:11,500 you tell it to stop. 5 00:00:11,560 --> 00:00:14,800 A for loop will loop through all the items in a list, and 6 00:00:14,810 --> 00:00:16,930 when it gets the end, it just stops automatically. 7 00:00:16,960 --> 00:00:20,620 That's really nice, but a while loop will run until you tell 8 00:00:20,630 --> 00:00:21,670 it to stop. 9 00:00:21,910 --> 00:00:25,450 Now you have to be careful with these because while loops 10 00:00:25,460 --> 00:00:28,240 will run forever, and depending on your settings on your 11 00:00:28,250 --> 00:00:30,940 computer, that could end up eating a lot of memory on your 12 00:00:30,950 --> 00:00:32,170 computer and end up crashing it. 13 00:00:32,180 --> 00:00:33,610 So just be careful with that one. 14 00:00:33,610 --> 00:00:35,400 So let's go over the syntax of this. 15 00:00:35,400 --> 00:00:39,900 Generally, it's just while something is True, do something, 16 00:00:39,900 --> 00:00:43,600 else. That's all this is going to do, and you can see that 17 00:00:43,650 --> 00:00:46,190 these are all highlighted words because these are all keywords. 18 00:00:46,200 --> 00:00:47,240 So it's a while loop, 19 00:00:47,240 --> 00:00:49,980 while anything is True, run some code. 20 00:00:49,990 --> 00:00:53,610 So let's do an example here where the 'num = 21 00:00:53,620 --> 00:00:59,800 100'. We could say 'while > 0: 22 00:01:00,820 --> 00:01:04,769 print(num)', and then also because this is going to 23 00:01:04,780 --> 00:01:07,530 run forever, because right now 'num = 100', it will 24 00:01:07,540 --> 00:01:09,120 always be greater than 0. 25 00:01:09,380 --> 00:01:12,260 So instead we have to set this manually. 26 00:01:12,260 --> 00:01:16,100 So we say 'num = num - 1', 27 00:01:16,300 --> 00:01:19,500 or actually we could do '-5' if we wanted to. 28 00:01:19,500 --> 00:01:21,900 We don't have to increment or decrement by 1. 29 00:01:21,900 --> 00:01:25,000 So this is going to print 100, and then it's going to say 30 00:01:25,120 --> 00:01:27,780 100 = 100 - 5, 31 00:01:27,790 --> 00:01:34,190 so it's 95, then print 95, then 90, then goes over, 85, 80, all 32 00:01:34,200 --> 00:01:37,600 the way down until that number continues to be True, till it 33 00:01:37,600 --> 00:01:39,500 is greater than 0. 34 00:01:39,560 --> 00:01:42,320 So let's go ahead and run this, and we see it starts at 100, 35 00:01:42,320 --> 00:01:45,300 goes all the way down to 5. 36 00:01:45,310 --> 00:01:47,890 The reason it doesn't show 0 is because we didn't say 37 00:01:48,120 --> 00:01:51,060 'while num >= 0', 38 00:01:51,070 --> 00:01:53,790 we said as long as it's greater than 0. If we wanted 39 00:01:53,800 --> 00:01:58,200 0 to be in there, we could just change it to a '>=' sign, 40 00:01:58,200 --> 00:02:00,600 and that's not going to work because I have to reassign 41 00:02:00,600 --> 00:02:02,200 number up here. There we go. 42 00:02:03,900 --> 00:02:05,900 And now we see 0 at the bottom. 43 00:02:05,960 --> 00:02:09,830 Now, if you ever get stuck in a while loop, generally you 44 00:02:09,840 --> 00:02:13,490 can just hit 'Control + C' on your computer, especially if you're 45 00:02:13,500 --> 00:02:15,260 just running something in the terminal, this is, 46 00:02:15,460 --> 00:02:17,890 yeah, this is my Jupyter Notebook running. 47 00:02:17,890 --> 00:02:20,710 But if you're running your code in your terminal, and you 48 00:02:20,720 --> 00:02:23,140 just see your code is just running the same thing over and over and 49 00:02:23,150 --> 00:02:26,200 over again, forever and ever, just hit 'Control + C', 50 00:02:26,320 --> 00:02:27,940 and it should usually quit 51 00:02:27,950 --> 00:02:31,130 it. Might take a couple seconds for it to kick in, but usually 52 00:02:31,280 --> 00:02:32,570 that's all you have to do. 53 00:02:32,580 --> 00:02:35,720 Typically, people like to stick with for loops just because 54 00:02:35,730 --> 00:02:38,780 a for loop will automatically stop, and we're usually looping 55 00:02:38,780 --> 00:02:41,400 through things that are predefined anyways. 56 00:02:41,400 --> 00:02:44,370 But there are cases where you want to use a while loop. 57 00:02:44,380 --> 00:02:48,240 For example, if you wanted to randomly create 100 users on 58 00:02:48,250 --> 00:02:51,120 your website, you would do a number of users is equal to 59 00:02:51,130 --> 00:02:54,030 0, and then while number of users is less than or equal 60 00:02:54,040 --> 00:02:57,850 to 100, create some users in here, and then do a number of 61 00:02:57,860 --> 00:03:00,280 users is equal to number of users plus one, and that will 62 00:03:00,280 --> 00:03:01,780 just create 100 users for you. 63 00:03:01,840 --> 00:03:04,960 So you see, there are times when you want to use a while 64 00:03:04,970 --> 00:03:07,500 loop. Generally speaking, though, at least in my experience, 65 00:03:07,500 --> 00:03:11,200 you see for loops a lot more frequently than you do while loops.