1 00:00:00,530 --> 00:00:02,960 Hello welcome to this video. 2 00:00:02,990 --> 00:00:12,170 In this video I'm going to introduce you to while loop statements in Python while loop statements are 3 00:00:12,170 --> 00:00:16,730 the type of statements that you add to your code. 4 00:00:16,730 --> 00:00:26,630 If you want certain aspects of your code to run continuously based on certain conditions that are true. 5 00:00:26,630 --> 00:00:34,670 One thing you need to be careful of with the while loop is that you must make sure that the condition 6 00:00:35,060 --> 00:00:43,430 will eventually become false if not be called will continue to run and run and it becomes an infinite 7 00:00:43,640 --> 00:00:44,400 loop. 8 00:00:44,510 --> 00:00:53,350 So you need to set it up in a way that the loop can be terminated when the condition becomes false. 9 00:00:53,360 --> 00:01:02,210 You can also use an else clause on L statement inside your while loop so you can wrap the else statement 10 00:01:02,300 --> 00:01:04,340 inside a while. 11 00:01:04,340 --> 00:01:08,810 Look this is what a basic syntax looks like for a while loop. 12 00:01:09,010 --> 00:01:18,620 You basically have the wow and then you set the condition and then you have the code that will execute. 13 00:01:19,280 --> 00:01:27,650 So let's create a while loop statement to 18 an index variable a variable that the while loop will use 14 00:01:27,920 --> 00:01:29,360 to get started. 15 00:01:29,720 --> 00:01:37,390 So I'm going to create a variable and when I call it I and I'm going to give it a value of 1. 16 00:01:38,180 --> 00:01:45,890 So now that I've got the initial variable set this is a variable that the while loop will work with. 17 00:01:46,400 --> 00:01:49,260 So you start the while loop by using the keyword. 18 00:01:49,370 --> 00:01:50,420 Wow. 19 00:01:50,450 --> 00:01:54,600 Then you specify the variable which is I. 20 00:01:54,650 --> 00:01:56,150 So I'm setting the condition. 21 00:01:56,150 --> 00:02:07,940 Now I'm saying while I wish I said to 1 while it is less than 7 I'll add my colon. 22 00:02:07,940 --> 00:02:12,330 Then I will specify what I want to happen. 23 00:02:12,380 --> 00:02:17,360 So this is the code that execute is going to print. 24 00:02:17,400 --> 00:02:25,190 I on the screen now the way the code is at the moment. 25 00:02:25,280 --> 00:02:30,330 If I stop here this will become an infinite loop. 26 00:02:30,350 --> 00:02:39,140 That means it will keep running and it will never stop because as it is now the condition will always 27 00:02:39,140 --> 00:02:44,420 be true because I will always be less than seven. 28 00:02:44,480 --> 00:02:54,020 So what I need to do I need to use an increment operator to keep increasing the value by one. 29 00:02:54,020 --> 00:03:04,020 So each time the loop goes round I wanted to increase it by one until it's becomes less than seven. 30 00:03:04,040 --> 00:03:11,000 So what I need to do here I need to see I which is the variable i need to add plus equals to. 31 00:03:11,030 --> 00:03:17,470 OPERATOR What this operator would do a tool each time the loop goes round. 32 00:03:17,540 --> 00:03:24,060 It will add one to it each time on till this condition becomes false. 33 00:03:24,080 --> 00:03:31,220 So if he keeps going around and adding one eventually this will become less than seven and then the 34 00:03:31,220 --> 00:03:35,450 loop will terminate and jump out of this block. 35 00:03:35,750 --> 00:03:37,560 And that will end the loop. 36 00:03:37,580 --> 00:03:47,150 But if I don't put this bit on line five then the code on line three and four will keep running and 37 00:03:47,150 --> 00:03:48,050 never stop. 38 00:03:48,050 --> 00:03:50,850 It will become an infinite loop. 39 00:03:50,860 --> 00:03:57,010 There is something that you always have to be careful about when you are using a while loop. 40 00:03:57,020 --> 00:04:04,260 You need to make sure that at some point the code will become force. 41 00:04:04,280 --> 00:04:06,170 That is the condition we become. 42 00:04:06,200 --> 00:04:09,440 So what will happen now if I play this code. 43 00:04:09,710 --> 00:04:14,210 It will print out one to six. 44 00:04:14,240 --> 00:04:16,190 I've got my code saved. 45 00:04:16,190 --> 00:04:18,980 I'm just going to play the code so you can see the output. 46 00:04:19,040 --> 00:04:26,820 What should happen this code here on line 4 will print the numbers 1 2 6. 47 00:04:27,130 --> 00:04:35,390 It will not print seven because the condition says as long as while this variable i is less than seven 48 00:04:35,630 --> 00:04:41,330 I wanted to print I saw what this code on low five does each time the loop goes round. 49 00:04:41,330 --> 00:04:45,310 It will add one to it until it gets to six. 50 00:04:45,300 --> 00:04:51,170 It then terminates the loop because the condition no longer becomes true. 51 00:04:51,170 --> 00:04:59,660 So let me play that click play and if you look here we can see inside my console I'm printing the number 52 00:04:59,660 --> 00:05:03,140 one two three four five and six. 53 00:05:03,140 --> 00:05:04,520 It doesn't print seven. 54 00:05:04,700 --> 00:05:09,050 So that is the condition I have set. 55 00:05:09,050 --> 00:05:12,670 As long as i is less than seven I wanted to print i. 56 00:05:12,680 --> 00:05:17,540 So each time it goes round your prints one because that one is initial. 57 00:05:17,540 --> 00:05:18,910 It goes round the loop again. 58 00:05:18,920 --> 00:05:21,620 It repeats and I had one here. 59 00:05:21,620 --> 00:05:24,130 He prints to it does the same thing again. 60 00:05:24,170 --> 00:05:31,250 Each time it goes round it will print the number that has been added to the original value. 61 00:05:31,610 --> 00:05:34,200 So that's basically how the while loop works. 62 00:05:34,200 --> 00:05:37,010 But be careful when you set it up a while loop. 63 00:05:37,220 --> 00:05:46,760 Make sure that it will eventually terminate by evaluating the condition you have set to be false if 64 00:05:46,760 --> 00:05:47,740 you have a wild look. 65 00:05:47,740 --> 00:05:53,030 That condition is true all the time be called will not X will not terminate. 66 00:05:53,030 --> 00:05:55,830 It will keep running and running and running and running. 67 00:05:55,830 --> 00:05:57,800 So you really do not want that. 68 00:05:57,830 --> 00:06:02,300 So that's it for this video on while loop statements. 69 00:06:02,300 --> 00:06:03,200 Thanks for watching. 70 00:06:03,350 --> 00:06:03,980 Bye for now.