1 00:00:00,610 --> 00:00:04,810 So when should you use a while loop and when should you use a for loop? 2 00:00:05,440 --> 00:00:08,830 And it really depends on the problem you're trying to solve. 3 00:00:09,700 --> 00:00:20,380 For example, in a for loop I could do for item in, let's say, a list, one, two, three. 4 00:00:20,830 --> 00:00:25,720 And just looking at this code, you know, right away that it's going to be looped over three times. 5 00:00:25,750 --> 00:00:26,110 Right. 6 00:00:26,900 --> 00:00:33,930 And I could say here, print item, but he can do this in a while loop as well. 7 00:00:34,870 --> 00:00:47,380 I can say I equals to zero, and then while I is less than, let's say, the length of the list, that 8 00:00:47,380 --> 00:00:49,030 is one, two, three. 9 00:00:50,230 --> 00:00:58,450 Well, in that case, I want you to print I or the item and then increment I by one. 10 00:01:00,950 --> 00:01:02,120 If I run this. 11 00:01:03,540 --> 00:01:08,940 I see that, well, technically here were incrementing the eye, which is the value, but let's say 12 00:01:08,940 --> 00:01:10,820 we had a list up here. 13 00:01:11,280 --> 00:01:15,590 So my list and this list will be one, two, three. 14 00:01:16,830 --> 00:01:24,690 Well, my list can be iterated with a for loop or can be iterated with a while loop. 15 00:01:24,690 --> 00:01:27,450 And we simply say my list. 16 00:01:28,500 --> 00:01:32,730 At index of I and if I run this. 17 00:01:34,430 --> 00:01:36,040 I get the same results. 18 00:01:37,800 --> 00:01:40,260 But which one do you think is better? 19 00:01:41,960 --> 00:01:49,750 First off, while loops are very flexible, we can do a lot because we have this conditional statement, 20 00:01:49,760 --> 00:01:52,700 we can loop more than three times if we really wanted to. 21 00:01:52,760 --> 00:01:57,980 So in that case, while loops are more powerful, but for loops are simpler, right. 22 00:01:57,980 --> 00:02:02,570 Like this code reads really nicely and really well, it makes sense. 23 00:02:02,580 --> 00:02:07,520 We just want to loop over something that we already know how many times we want to loop over three times 24 00:02:07,910 --> 00:02:08,810 with a wire loop. 25 00:02:08,810 --> 00:02:13,520 We have to create this variable and we have to make sure we remember to increment the variable so we 26 00:02:13,520 --> 00:02:14,840 don't get an infinite loop. 27 00:02:15,860 --> 00:02:20,450 So with a while loop, you need to make sure we remember to halt the loop at some point. 28 00:02:21,290 --> 00:02:29,150 So my rule is usually this for simple loops or iterating over iterable. 29 00:02:29,180 --> 00:02:31,520 Objects for loops are great. 30 00:02:31,880 --> 00:02:35,840 But let's say you're not sure how many times you want to loop over something. 31 00:02:36,020 --> 00:02:39,710 You're not really sure how long it's going to take. 32 00:02:40,600 --> 00:02:46,000 For looping, so you want to say while something is true, just keep looping. 33 00:02:47,010 --> 00:02:53,460 For example, let's say we're trying to go through an email list that we've collected on a website and 34 00:02:53,460 --> 00:02:56,660 for each email list, we want to send an email. 35 00:02:57,450 --> 00:03:03,420 Well, while the list is still there, let's just keep sending emails. 36 00:03:04,080 --> 00:03:11,040 There are many, many cases, but one of the most useful ways to use the while loop is like this. 37 00:03:13,750 --> 00:03:15,250 It's to say while. 38 00:03:18,640 --> 00:03:19,510 Do something. 39 00:03:21,150 --> 00:03:24,480 And make sure that at the end we break. 40 00:03:26,410 --> 00:03:28,120 Hold on, what's happening here? 41 00:03:29,140 --> 00:03:34,720 I'm saying, while true, wouldn't that create an infinite loop, I mean, true is always going to be 42 00:03:34,720 --> 00:03:37,560 true and you'd be right if you noticed that. 43 00:03:37,780 --> 00:03:40,030 But again, we have a brake statement here. 44 00:03:40,030 --> 00:03:44,170 So at the end, after we run line three, it's going to break. 45 00:03:45,310 --> 00:03:52,000 But now we can do something powerful like input here, and if you remember, an input is going to ask 46 00:03:52,000 --> 00:03:58,060 us for a prompt to enter something so I can say, hey, input, say something. 47 00:03:59,320 --> 00:04:00,700 And if I click run. 48 00:04:01,990 --> 00:04:06,880 It's going to say, hey, say something, you know, let's add semicolon here or call in here and let's 49 00:04:06,880 --> 00:04:08,800 try that again and look at that. 50 00:04:08,800 --> 00:04:13,120 It's saying say something, I'm on line three right now, so I'm going to say hi. 51 00:04:16,170 --> 00:04:20,490 And do you see that we just broke out of the loop? 52 00:04:21,270 --> 00:04:25,940 It only asked me once, even though this was true, as soon as we got to line four, I was done. 53 00:04:26,730 --> 00:04:28,770 But what if I remove this? 54 00:04:32,700 --> 00:04:41,370 If I say something, I say hi and then it asks me again, Hi, hi, I'm telling hi and it keeps asking 55 00:04:41,370 --> 00:04:42,540 me, asking me, asks me. 56 00:04:42,750 --> 00:04:44,970 And we have an infinite loop. 57 00:04:45,480 --> 00:04:49,350 But the interesting thing here is that we can use conditional logic. 58 00:04:49,350 --> 00:04:49,610 Right. 59 00:04:49,830 --> 00:04:50,790 What if we said. 60 00:04:51,880 --> 00:04:52,720 Response. 61 00:04:54,130 --> 00:05:05,680 Here and collect whatever the responses and say if response is equal to, let's say by then I'm going 62 00:05:05,680 --> 00:05:07,150 to break. 63 00:05:08,270 --> 00:05:10,250 So that if I run this again. 64 00:05:11,400 --> 00:05:17,070 I say he keeps asking me, and you know what, I'm getting annoyed with the machine, I'm going to say 65 00:05:17,070 --> 00:05:17,430 bye. 66 00:05:18,000 --> 00:05:20,540 It's going to exit it out for me. 67 00:05:21,270 --> 00:05:22,870 How cool is that? 68 00:05:23,550 --> 00:05:30,510 So while loops are extremely useful for tasks like this, where looping can happen for a long time, 69 00:05:30,510 --> 00:05:32,700 you don't know how many times it's going to happen. 70 00:05:32,970 --> 00:05:38,040 But this is something that you're just going to have to get used to with practice eventually. 71 00:05:38,190 --> 00:05:43,910 And I promise you, this happens where you'll figure out when to use while when to use for loop. 72 00:05:44,010 --> 00:05:47,250 But at the end of the day, use whatever solves your problem. 73 00:05:47,580 --> 00:05:48,490 I'll see you the next one. 74 00:05:49,050 --> 00:05:49,410 Bye bye.