1 00:00:00,000 --> 00:00:04,000 So there are two kinds of loops you can use in Python. 2 00:00:04,001 --> 00:00:08,000 The for loop, which you just discovered here in the previous lesson, 3 00:00:08,001 --> 00:00:12,000 and the while loop, which we are going to see now. 4 00:00:12,001 --> 00:00:16,000 And you will see that the knowledge you got when working 5 00:00:16,001 --> 00:00:18,000 on conditional statements will be very useful here. 6 00:00:18,001 --> 00:00:21,000 To explain how while loops work, 7 00:00:21,001 --> 00:00:25,000 I'm first going to do the exact same thing as for the loop here. 8 00:00:25,001 --> 00:00:29,000 So we are going to print hello with a counter. 9 00:00:29,001 --> 00:00:33,000 But this time using while, just going to put back to 10, 10 00:00:33,001 --> 00:00:37,000 because that's going to be a simpler to read. 11 00:00:37,001 --> 00:00:41,000 So just follow along and I'm going to explain that. 12 00:00:41,001 --> 00:00:46,000 I'm going to create first a variable i is equal to 0. 13 00:00:46,001 --> 00:00:54,000 And then I'm going to do while i is lower than 10, strictly lower than 10. 14 00:00:54,001 --> 00:00:58,000 I put a colon and then I do print hello. 15 00:00:59,000 --> 00:01:02,000 And then plus str i. 16 00:01:02,001 --> 00:01:08,000 Okay, so let's bring the same thing and I do i plus equal 1. 17 00:01:08,001 --> 00:01:12,000 Let's actually comment those two lines. 18 00:01:12,001 --> 00:01:15,000 Okay, so you can see I just comment like this. 19 00:01:15,001 --> 00:01:18,000 So those will not be executed. 20 00:01:18,001 --> 00:01:23,000 I'm running the programs and you can see we have hello zero until hello nine. 21 00:01:23,001 --> 00:01:26,655 So this is the exact same behavior with this 22 00:01:26,667 --> 00:01:30,000 and with this we have the same behavior. 23 00:01:30,001 --> 00:01:32,000 So now how does it work? 24 00:01:32,001 --> 00:01:34,079 Well, the while loop is different here because 25 00:01:34,091 --> 00:01:38,000 with the while you are going to test here, this is a conditional statement. 26 00:01:38,001 --> 00:01:43,000 Okay, so the while loop is basically the same as if structure. 27 00:01:43,001 --> 00:01:47,000 The difference is that while this condition is true, 28 00:01:47,001 --> 00:01:51,000 this block of code will continue to be executed. 29 00:01:51,001 --> 00:01:54,000 Okay, so you check if that is true. 30 00:01:54,001 --> 00:01:57,000 If it's true, yes, then you execute this. 31 00:01:57,001 --> 00:01:59,000 You come back to line five. 32 00:01:59,001 --> 00:02:01,000 You check again if this is true. 33 00:02:01,001 --> 00:02:05,000 If yes, you continue to execute this, etc. 34 00:02:05,001 --> 00:02:06,000 etc. 35 00:02:06,001 --> 00:02:10,000 And then when we come back to the line five and if this condition is false, 36 00:02:10,001 --> 00:02:13,021 in that case, we don't enter the block of code 37 00:02:13,033 --> 00:02:16,000 and re-resume the program here at line eight. 38 00:02:16,001 --> 00:02:18,000 So what did I do here? 39 00:02:18,001 --> 00:02:22,000 I created first a variable i that we're going to use as a counter. 40 00:02:22,001 --> 00:02:28,000 Now I'm going to check while i is strictly lower than 10, we continue to execute this. 41 00:02:28,001 --> 00:02:30,000 So at first, i is equal to zero. 42 00:02:30,001 --> 00:02:36,000 So i is... So zero is lower than 10, yes, we print hello plus zero. 43 00:02:36,001 --> 00:02:38,000 And then what I'm going to do, I need to do 44 00:02:38,001 --> 00:02:42,000 that, I'm going to add one to the counter, okay? 45 00:02:42,001 --> 00:02:44,000 So I'm going to add one every time. 46 00:02:44,001 --> 00:02:49,000 To add one, so you can actually do i is equal to i plus one, okay? 47 00:02:49,001 --> 00:02:54,000 But there is a shortcut, you can do plus equal. 48 00:02:54,001 --> 00:02:59,000 And this is simply going to add the element on the right to the variable on the left. 49 00:02:59,001 --> 00:03:04,000 So now i is equal to one, I come back to line five. 50 00:03:04,001 --> 00:03:07,000 One is strictly lower than 10, yes, we print hello one. 51 00:03:07,001 --> 00:03:13,000 And then I put plus one, so i is not equal to two, etc. etc. 52 00:03:13,001 --> 00:03:17,000 At one point, i will be equal to nine. 53 00:03:17,001 --> 00:03:21,000 So nine is lower, strictly lower than 10, yes. 54 00:03:21,001 --> 00:03:25,000 We print hello nine, I do nine plus one. 55 00:03:25,001 --> 00:03:29,000 So we get 10, no, i is equal to 10. 56 00:03:29,001 --> 00:03:34,000 We come back to line five and then is 10 strictly lower than 10? 57 00:03:34,001 --> 00:03:35,000 No, this is false. 58 00:03:35,001 --> 00:03:38,000 So we stop the while loop. 59 00:03:38,001 --> 00:03:43,000 Okay, and the condition here for the while can be anything you want, okay? 60 00:03:43,001 --> 00:03:47,000 Here, this is a very classic example with a counter. 61 00:03:47,001 --> 00:03:51,000 But as an example, let's say you read the temperature of your computer, okay? 62 00:03:51,001 --> 00:03:53,864 And you want to continue doing an action while 63 00:03:53,876 --> 00:03:57,000 the temperature is lower than the threshold, okay? 64 00:03:57,001 --> 00:04:01,000 You continue, you continue, and then you continue to read also the temperature. 65 00:04:01,001 --> 00:04:06,000 And when the temperature starts to get above the threshold 66 00:04:06,001 --> 00:04:08,000 that you have set, then you stop executing the while loop. 67 00:04:08,001 --> 00:04:11,000 All right, and now the one million dollar question. 68 00:04:12,000 --> 00:04:15,000 When to use a for loop versus when to use a while loop? 69 00:04:15,001 --> 00:04:18,673 So basically, use a for loop when you know exactly 70 00:04:18,685 --> 00:04:22,000 how many times you want to execute an action. 71 00:04:22,001 --> 00:04:25,000 Use a while loop when you don't know how many times, 72 00:04:25,001 --> 00:04:29,000 or for example, if the condition to continue is external to the loop. 73 00:04:29,001 --> 00:04:34,000 As an example, let's say you want to ask a user to give you some numbers. 74 00:04:34,001 --> 00:04:39,000 In the first scenario, you want exactly 10 numbers. 75 00:04:39,001 --> 00:04:41,000 So you are going to use a for loop and ask 10 times. 76 00:04:41,001 --> 00:04:47,000 In the second scenario, you allow the user to give as many numbers as they want, 77 00:04:47,001 --> 00:04:51,000 and you stop asking whenever the user gives you the number zero. 78 00:04:51,001 --> 00:04:54,000 Here, you are going to use a while loop. 79 00:04:54,001 --> 00:04:58,000 And don't worry if you still have some confusion about this. 80 00:04:58,001 --> 00:05:02,000 In many cases, both options are actually possible, 81 00:05:02,001 --> 00:05:05,000 just with a different way of writing the code. 82 00:05:05,001 --> 00:05:10,000 With a bit of experience, the choice will become much easier for you. 83 00:05:10,001 --> 00:05:15,000 And lastly, if we come back to the example here, the exact example I've used here, 84 00:05:15,001 --> 00:05:20,000 you can see that, well, here the for loop was actually more appropriate, 85 00:05:20,001 --> 00:05:25,000 because we knew exactly how many times we wanted to print the text.