1 00:00:00,000 --> 00:00:02,790 Narrator: So when should you use a while loop 2 00:00:02,790 --> 00:00:05,490 and when should you use a for loop? 3 00:00:05,490 --> 00:00:09,720 And it really depends on the problem you're trying to solve. 4 00:00:09,720 --> 00:00:12,870 For example, in a for loop 5 00:00:12,870 --> 00:00:16,140 I could do for item in, 6 00:00:16,140 --> 00:00:20,850 let's say a list, 1, 2, 3. 7 00:00:20,850 --> 00:00:23,520 And just looking at this code, you know right away 8 00:00:23,520 --> 00:00:26,910 that it's going to be looped over three times, right? 9 00:00:26,910 --> 00:00:31,080 And I could say here, print item. 10 00:00:31,080 --> 00:00:34,023 But you can do this in a while loop as well. 11 00:00:34,920 --> 00:00:38,640 I can say i equals to 0. 12 00:00:38,640 --> 00:00:42,030 And then while i is less 13 00:00:42,030 --> 00:00:47,030 than, let's say the length of the list, 14 00:00:47,130 --> 00:00:49,143 that is 1, 2, 3. 15 00:00:50,220 --> 00:00:53,640 Well, in that case I want you to print i 16 00:00:53,640 --> 00:00:58,563 or the item and then increment i by 1. 17 00:01:00,930 --> 00:01:05,850 If I run this, I see that, well technically here 18 00:01:05,850 --> 00:01:08,370 we're incrementing the i, which is the value, 19 00:01:08,370 --> 00:01:11,280 but let's say we had a list up here. 20 00:01:11,280 --> 00:01:15,753 So my_list, and this list will be 1, 2, 3. 21 00:01:16,860 --> 00:01:21,860 Well, my_list can be iterated with a for loop 22 00:01:22,140 --> 00:01:24,660 or it can be iterated with a while loop. 23 00:01:24,660 --> 00:01:26,733 And we simply say, my_list, 24 00:01:28,710 --> 00:01:31,350 at index of i. 25 00:01:31,350 --> 00:01:36,213 And if I run this, I get the same results. 26 00:01:37,830 --> 00:01:40,443 But, which one do you think is better? 27 00:01:41,940 --> 00:01:46,770 First off, while loops are very flexible. 28 00:01:46,770 --> 00:01:49,710 We can do a lot because we have this conditional statement, 29 00:01:49,710 --> 00:01:52,740 we can loop more than three times if we really wanted to. 30 00:01:52,740 --> 00:01:55,410 So in that case, while loops are more powerful 31 00:01:55,410 --> 00:01:57,960 but for loops are simpler, right? 32 00:01:57,960 --> 00:02:01,410 Like this code reads really nicely and really well, 33 00:02:01,410 --> 00:02:02,580 it makes sense. 34 00:02:02,580 --> 00:02:05,070 We just wanna loop over something that we already 35 00:02:05,070 --> 00:02:06,600 know how many times we wanna loop 36 00:02:06,600 --> 00:02:07,920 over, three times. 37 00:02:07,920 --> 00:02:10,500 With a while loop. We have to create this variable 38 00:02:10,500 --> 00:02:12,120 and we have to make sure we remember 39 00:02:12,120 --> 00:02:13,260 to increment the variable 40 00:02:13,260 --> 00:02:14,943 so we don't get an infinite loop. 41 00:02:15,870 --> 00:02:18,870 So with a while loop, you need to make sure we remember to 42 00:02:18,870 --> 00:02:21,300 halt the loop at some point. 43 00:02:21,300 --> 00:02:24,660 So my rule is usually this. 44 00:02:24,660 --> 00:02:29,660 For simple loops or iterating over iterable objects 45 00:02:30,180 --> 00:02:31,890 for loops are great. 46 00:02:31,890 --> 00:02:35,190 But let's say you are not sure how many times you wanna loop 47 00:02:35,190 --> 00:02:38,610 over something, you're not really sure how long 48 00:02:38,610 --> 00:02:42,450 it's going to take for looping. 49 00:02:42,450 --> 00:02:46,083 So you wanna say while something is true, just keep looping. 50 00:02:47,010 --> 00:02:50,790 For example, let's say we're trying to go through an email 51 00:02:50,790 --> 00:02:53,040 list that we've collected on a website. 52 00:02:53,040 --> 00:02:57,480 And for each email list we wanna send an email, 53 00:02:57,480 --> 00:03:01,200 Well, while the list is still there, 54 00:03:01,200 --> 00:03:04,050 let's just keep sending emails. 55 00:03:04,050 --> 00:03:05,970 There are many, many cases, 56 00:03:05,970 --> 00:03:08,280 but one of the most useful ways 57 00:03:08,280 --> 00:03:11,443 to use the while loop is like this is. 58 00:03:13,730 --> 00:03:18,730 Is to say, while true, do something. 59 00:03:21,210 --> 00:03:24,603 And make sure that at the end we break. 60 00:03:26,430 --> 00:03:28,233 Hold on what's happening here? 61 00:03:29,160 --> 00:03:30,720 I'm saying while true 62 00:03:30,720 --> 00:03:32,460 wouldn't that create an infinite loop? 63 00:03:32,460 --> 00:03:35,700 I mean, true is always going to be true. 64 00:03:35,700 --> 00:03:37,800 And you'd be right if you notice that. 65 00:03:37,800 --> 00:03:39,990 But again, we have a break statement here, 66 00:03:39,990 --> 00:03:44,283 so at the end, after we run line three, it's going to break. 67 00:03:45,330 --> 00:03:49,590 But now we can do something powerful like input here. 68 00:03:49,590 --> 00:03:52,830 And if you remember an input is going to ask us for a prompt 69 00:03:52,830 --> 00:03:54,510 to enter something. 70 00:03:54,510 --> 00:03:58,173 So I can say, hey, input, say something. 71 00:03:59,340 --> 00:04:03,570 And if I click run, it's going to say, hey, say something. 72 00:04:03,570 --> 00:04:05,580 You know what? Let's add semicolon here 73 00:04:05,580 --> 00:04:08,310 or a colon here and let's try that again. 74 00:04:08,310 --> 00:04:10,050 And look at that. It's saying say something. 75 00:04:10,050 --> 00:04:13,263 I'm on line three right now, so I'm going to say hi. 76 00:04:16,200 --> 00:04:21,200 And did you see that? We just broke out of the loop? 77 00:04:21,269 --> 00:04:23,610 It only asked me once, even though this was true 78 00:04:23,610 --> 00:04:26,760 as soon as we got to line four, I was done. 79 00:04:26,760 --> 00:04:31,113 But what if I removed this and I click run. 80 00:04:32,730 --> 00:04:34,650 If I say something, I say hi. 81 00:04:34,650 --> 00:04:39,650 And then it asks me again, Hi, Hi. I'm telling hi. 82 00:04:40,260 --> 00:04:42,750 And it keeps asking me, asking me, asking me, 83 00:04:42,750 --> 00:04:45,480 and we have an infinite loop. 84 00:04:45,480 --> 00:04:47,310 But the interesting thing here is 85 00:04:47,310 --> 00:04:49,830 that we can use conditional logic, right? 86 00:04:49,830 --> 00:04:54,830 What if we said response here 87 00:04:55,110 --> 00:04:57,960 and collect whatever the response is and say 88 00:04:57,960 --> 00:05:02,960 if response is equal to, let's say bye 89 00:05:04,170 --> 00:05:07,293 then I'm going to break. 90 00:05:08,280 --> 00:05:10,413 So that if I run this again, 91 00:05:11,430 --> 00:05:14,910 I say hi keeps asking me, and you know what? 92 00:05:14,910 --> 00:05:18,060 I'm getting annoyed with you machine. I'm going to say bye. 93 00:05:18,060 --> 00:05:21,270 It's going to exit it out for me. 94 00:05:21,270 --> 00:05:23,550 How cool is that? 95 00:05:23,550 --> 00:05:27,390 So while loops are extremely useful for tasks 96 00:05:27,390 --> 00:05:30,480 like this where looping can happen for a long time 97 00:05:30,480 --> 00:05:33,000 you don't know how many times it's going to happen. 98 00:05:33,000 --> 00:05:34,920 But this is something that you're just going to 99 00:05:34,920 --> 00:05:37,170 have to get used to with practice. 100 00:05:37,170 --> 00:05:39,030 Eventually, and I promise you 101 00:05:39,030 --> 00:05:41,490 this happens where you'll figure out 102 00:05:41,490 --> 00:05:44,040 when to use while, when to use for loop. 103 00:05:44,040 --> 00:05:47,610 But at the end of the day, use whatever solves your problem. 104 00:05:47,610 --> 00:05:49,510 I'll see you in the next one. Bye-bye.