1 00:00:00,000 --> 00:00:03,299 So there are two kinds of loops you can use in 2 00:00:03,299 --> 00:00:06,929 Python, the for loop, which you just discovered 3 00:00:06,929 --> 00:00:10,049 here in the previous lesson, and the while loop, 4 00:00:10,079 --> 00:00:12,599 which we are going to see now. And you will see 5 00:00:12,599 --> 00:00:14,879 that the knowledge you got when working on 6 00:00:14,879 --> 00:00:18,959 conditional statements will be very useful here to 7 00:00:18,989 --> 00:00:22,319 explain how while loops work, answer's going to 8 00:00:22,319 --> 00:00:25,799 do the exact same thing. As for the looping, so 9 00:00:25,799 --> 00:00:29,669 we're going to print Hello with a counter. But 10 00:00:29,759 --> 00:00:32,279 this time using while, okay, just going to put 11 00:00:32,279 --> 00:00:35,819 back 10, okay, because that's going to be simpler 12 00:00:35,819 --> 00:00:38,759 to read. So just follow along, and I'm going to 13 00:00:39,209 --> 00:00:42,839 explain that, okay. I'm going to do create first 14 00:00:42,869 --> 00:00:47,969 variable, i is equal to zero, okay. And then I'm 15 00:00:47,969 --> 00:00:52,409 going to do while i is lower than 10 strictly to 16 00:00:52,409 --> 00:00:55,859 one and 10. Okay, I put a colon, and then I do 17 00:00:55,889 --> 00:01:03,149 print, Hello, and then plus str(i). Okay, so let's 18 00:01:03,149 --> 00:01:08,519 print the same thing. And I do i plus equal one. 19 00:01:09,089 --> 00:01:13,439 Let's actually comment, those two lines. Ok. So as 20 00:01:13,439 --> 00:01:16,379 you can see just comment, like this, so those 21 00:01:16,409 --> 00:01:20,159 will not be executed. I'm running the program, 22 00:01:20,159 --> 00:01:23,309 and you can see we have Hello 0 until Hello 23 00:01:23,309 --> 00:01:26,579 9. So this is the exact same behavior with 24 00:01:26,579 --> 00:01:30,419 this, and with this, we have the same behavior. So 25 00:01:30,419 --> 00:01:33,179 now how does it work? Well, the while loop is 26 00:01:33,179 --> 00:01:35,189 different here, because we have the while, you are 27 00:01:35,189 --> 00:01:37,679 going to test here, this is a conditional 28 00:01:37,679 --> 00:01:40,439 statement. Okay. So the while loop is basically 29 00:01:40,439 --> 00:01:44,399 the same as if structure. The difference is that 30 00:01:44,819 --> 00:01:48,929 while this condition is true, this block of code 31 00:01:49,079 --> 00:01:53,069 will continue to be executed. Okay. So you check 32 00:01:53,170 --> 00:01:56,850 if that is true, it is true, yes. Then you execute this. 33 00:01:56,850 --> 00:02:00,060 You come back to line five, you check again if 34 00:02:00,090 --> 00:02:03,300 this is true, if yes, you continue to execute 35 00:02:03,300 --> 00:02:06,990 this, etc, etc. And then when we come back to the 36 00:02:06,990 --> 00:02:10,650 line five, and if this condition is false, in that 37 00:02:10,650 --> 00:02:13,650 case, we don't enter the block of code and rerun 38 00:02:13,800 --> 00:02:17,490 the program here at line eight. So what did I do 39 00:02:17,490 --> 00:02:20,850 here, I created first variable i that we're going 40 00:02:20,850 --> 00:02:24,000 to use as a counter. Now I'm going to check while 41 00:02:24,030 --> 00:02:27,420 i is strictly lower than 10. We continue to execute 42 00:02:27,420 --> 00:02:32,370 this. So at first i is equal to zero. So is zero 43 00:02:32,370 --> 00:02:35,460 is lower than 10. Yes, we print Hello plus zero. 44 00:02:35,880 --> 00:02:38,160 And then what I'm going to do, I need to do that, 45 00:02:38,460 --> 00:02:42,180 I'm going to add one to counter. Okay, so I'm 46 00:02:42,180 --> 00:02:45,360 going to add one every time. To add one, so you can 47 00:02:45,360 --> 00:02:50,490 actually do i is equal to i plus one, okay. But 48 00:02:50,520 --> 00:02:54,150 there is a shortcut, you can do plus equal and 49 00:02:54,150 --> 00:02:56,940 this is simply going to add the element on the 50 00:02:56,940 --> 00:03:00,990 right to the variable on the left. So now i is 51 00:03:00,990 --> 00:03:04,470 equal to one, I come back to line five, one is 52 00:03:04,470 --> 00:03:07,140 strictly lower then 10, yes, we bring Hello one, 53 00:03:07,530 --> 00:03:11,250 and then i put plus one, so i is not equal to two, 54 00:03:11,700 --> 00:03:17,160 etc, etc. At one point, i will be equal to nine. 55 00:03:17,430 --> 00:03:21,420 So nine is lower strictly lower than 10. Yes, we 56 00:03:21,420 --> 00:03:26,190 print Hello nine, I do nine plus one. So we get 57 00:03:26,250 --> 00:03:29,700 10. No, i is equal to 10. We come back to line 58 00:03:29,700 --> 00:03:33,990 five and then is 10 strictly lower than 10. No, 59 00:03:33,990 --> 00:03:39,060 this is false. So we stopped the while loop. Okay, 60 00:03:39,090 --> 00:03:41,580 and the condition here for the while can be 61 00:03:41,610 --> 00:03:44,640 anything you want. Okay, here, this is a very 62 00:03:44,640 --> 00:03:48,300 classic example with a counter. But as an example, 63 00:03:48,300 --> 00:03:50,370 let's say you read the temperature of your 64 00:03:50,370 --> 00:03:53,010 computer, okay. And you want to continue doing an 65 00:03:53,010 --> 00:03:55,920 action while the temperature is lower than the 66 00:03:55,920 --> 00:03:58,320 threshold. Okay, you continue, you continue, and 67 00:03:58,320 --> 00:04:00,450 then you continue to read also the temperature. 68 00:04:00,780 --> 00:04:04,020 And when the temperature starts to get above the 69 00:04:04,020 --> 00:04:06,720 threshold that you have set, then you stop 70 00:04:06,720 --> 00:04:09,870 executing the while. Alright, and now that 1 71 00:04:09,870 --> 00:04:13,803 million dollar question, when do you use for versus 72 00:04:13,803 --> 00:04:17,100 when to use a while loop. So basically use a 73 00:04:17,100 --> 00:04:19,800 for loop when you know exactly how many times you 74 00:04:19,800 --> 00:04:23,100 want to execute an action. Use a while loop when 75 00:04:23,100 --> 00:04:25,950 you don't know how many times are for example, if 76 00:04:25,950 --> 00:04:29,340 the condition to continue is external to the loop. 77 00:04:29,910 --> 00:04:33,030 As an example, let's say you want to ask a user to 78 00:04:33,030 --> 00:04:36,600 give you some numbers. In the first scenario, you 79 00:04:36,630 --> 00:04:39,600 want exactly 10 numbers. So you are going to use a 80 00:04:39,600 --> 00:04:43,500 for loop and ask 10 times. In the second scenario, 81 00:04:43,560 --> 00:04:46,560 you allow the user to give as many numbers as they 82 00:04:46,560 --> 00:04:50,190 want. And you stop asking whenever the user gives 83 00:04:50,190 --> 00:04:53,070 you the numbers zero. Here you are going to use a 84 00:04:53,070 --> 00:04:56,580 while loop. And don't worry if you still have some 85 00:04:56,580 --> 00:05:00,120 confusion about this. In many cases, both 86 00:05:00,150 --> 00:05:03,090 options are actually possible, just with a 87 00:05:03,090 --> 00:05:06,360 different way of writing the code. With a bit of 88 00:05:06,390 --> 00:05:09,150 experience, the choice will become much easier for 89 00:05:09,150 --> 00:05:12,180 you. And lastly, if we come back to the example 90 00:05:12,180 --> 00:05:15,090 here, the exact example I've used here, you can 91 00:05:15,090 --> 00:05:18,930 see that well here the for loop was actually more 92 00:05:18,960 --> 00:05:22,680 appropriate because we knew exactly how many times 93 00:05:22,710 --> 00:05:24,950 we wanted to print the text.