1 00:00:00,430 --> 00:00:02,080 All right guys, in this lesson 2 00:00:02,110 --> 00:00:07,110 I want to talk about the while loop, the loop that will continue going while a 3 00:00:07,810 --> 00:00:09,640 particular condition is true. 4 00:00:10,660 --> 00:00:15,100 This is a really simple version of a robot that's using a while loop. 5 00:00:15,520 --> 00:00:16,810 So in this case, 6 00:00:16,990 --> 00:00:21,580 the instruction would be while you are plugged into the wall and you have 7 00:00:21,580 --> 00:00:26,050 electricity move forwards. So this is what happens. 8 00:00:26,260 --> 00:00:31,260 It will stop once it pulls the socket out of the wall and it stops receiving 9 00:00:31,720 --> 00:00:34,990 electricity. Let's compare this against the 10 00:00:34,990 --> 00:00:39,190 for loop that we've seen before. So we've seen sort of two flavors of the 11 00:00:39,190 --> 00:00:43,660 for loop: one where we're looping through a list of items to do something with 12 00:00:43,690 --> 00:00:45,070 each item in the list, 13 00:00:45,490 --> 00:00:50,490 and the other is using a range function where we create a range between a and b 14 00:00:51,160 --> 00:00:55,990 and then we use every number in that range to do something. For example, 15 00:00:55,990 --> 00:01:00,610 in this case would be to print it out. The while loop looks 16 00:01:00,640 --> 00:01:03,520 something like this. And in this case, 17 00:01:03,880 --> 00:01:07,810 while this particular condition is true, 18 00:01:08,440 --> 00:01:12,400 then we go inside the loop and we do something repeatedly. 19 00:01:12,910 --> 00:01:17,290 And it's only when the something becomes false does the loop stop. 20 00:01:18,610 --> 00:01:22,120 So let's take a look at what the code would look like in real life. 21 00:01:22,570 --> 00:01:27,250 So here I have the Hurdle 1 challenge that you did previously. 22 00:01:27,850 --> 00:01:28,750 And for this 23 00:01:28,810 --> 00:01:33,810 we created a for loop in order to get our robot to jump over the hurdle six 24 00:01:34,450 --> 00:01:36,970 times to get to the final destination. 25 00:01:37,630 --> 00:01:42,010 Now we could also do the same thing, but using a while loop. 26 00:01:42,280 --> 00:01:43,540 And this is how we would do it. 27 00:01:43,930 --> 00:01:48,310 Let's say we create a variable called a number_of_hurdles, 28 00:01:48,730 --> 00:01:52,120 and we set that equal to six because there are six hurdles. 29 00:01:52,690 --> 00:01:57,690 And then we use a while loop to loop through these hurdles until this goes down 30 00:01:59,140 --> 00:02:02,800 to zero. So we can say while number_of_hurdles 31 00:02:02,850 --> 00:02:03,683 Right, 32 00:02:07,100 --> 00:02:11,390 > 0: 33 00:02:12,050 --> 00:02:14,360 and then we get it to do something. Well in this case, 34 00:02:14,360 --> 00:02:17,900 what we need it to do is to perform the jump function. 35 00:02:18,860 --> 00:02:23,150 And at the end of the loop, just before we go back to the beginning, 36 00:02:23,270 --> 00:02:26,270 we're going to decrease the number of hurdles, 37 00:02:26,390 --> 00:02:27,223 Right 38 00:02:28,700 --> 00:02:31,040 by one., -= 1. 39 00:02:31,730 --> 00:02:36,730 So now let's go ahead and delete this version using the for loop. And in order to 40 00:02:37,610 --> 00:02:39,350 better visualize what's happening here 41 00:02:39,350 --> 00:02:44,120 I'm actually also going to add a print statement to print that variable. 42 00:02:44,700 --> 00:02:45,533 Right? 43 00:02:47,930 --> 00:02:49,880 So now let's step through this. 44 00:02:50,300 --> 00:02:55,300 And the first time we enter the while loop, number_of_hurdles equals six, 45 00:02:55,430 --> 00:02:56,900 six is greater than zero. 46 00:02:57,200 --> 00:03:01,900 Then that is a go ahead to jump into the loop and execute these three lines of 47 00:03:01,900 --> 00:03:06,730 code. So now we go in and it performs the jump function. 48 00:03:09,430 --> 00:03:13,090 And then once that's done, it comes back, goes onto the next line, 49 00:03:13,450 --> 00:03:17,620 number_of_hurdles minus equals one, so six should become five. 50 00:03:17,860 --> 00:03:22,750 So now when it executes this print function, we should see five printed. 51 00:03:23,530 --> 00:03:28,270 So lets move this little pop up away on the side and let's keep going. 52 00:03:28,270 --> 00:03:31,900 So now it's gone back to the beginning of the while loop and it's testing this 53 00:03:31,900 --> 00:03:36,900 condition once more: is the number of hurdles, five, still greater than zero. 54 00:03:37,330 --> 00:03:38,770 Well, so if it's true, 55 00:03:38,860 --> 00:03:43,860 then it goes into the while loop again and performs all of those lines of code. 56 00:03:45,250 --> 00:03:49,000 So now it's minus one and it should now be four. 57 00:03:49,720 --> 00:03:54,720 And it keeps on going until the point where this condition number_of_hurdles 58 00:03:56,140 --> 00:03:59,020 greater than zero becomes false. 59 00:04:00,280 --> 00:04:05,280 So now this is the moment where we're going to subtract a further one from our 60 00:04:05,980 --> 00:04:08,650 number of hurdles, which is currently one. 61 00:04:08,980 --> 00:04:12,760 So that should take it down to zero. And once that's done, 62 00:04:13,120 --> 00:04:17,709 this condition is no longer true. Zero is not greater than zero. 63 00:04:18,040 --> 00:04:20,529 Zero is equal to zero, but it's not greater. 64 00:04:20,980 --> 00:04:25,980 So now this is false and we exit out of the while loop and we end our program. 65 00:04:27,700 --> 00:04:31,000 This is what the syntax for the while loop looks like. First, 66 00:04:31,030 --> 00:04:34,960 we have the while keyword and then we have some sort of condition that we're 67 00:04:34,960 --> 00:04:35,860 going to test. 68 00:04:36,130 --> 00:04:40,660 So previously you saw it was number of hurdles being greater than zero, 69 00:04:41,110 --> 00:04:43,240 and whenever that condition is true, 70 00:04:43,540 --> 00:04:48,070 then it's going to look inside the while loop at the indented lines of code to 71 00:04:48,070 --> 00:04:52,420 carry out those instructions, to do this, then do this, then do this. 72 00:04:52,720 --> 00:04:55,120 And finally, when it gets to the end of the while loop, 73 00:04:55,150 --> 00:04:59,080 it comes back to the beginning and tests this condition again. 74 00:04:59,230 --> 00:05:00,370 And if it's still true, 75 00:05:00,640 --> 00:05:05,230 then it's going to go through and loop and loop and loop until this condition 76 00:05:05,230 --> 00:05:09,310 becomes false at which point it ends an exits the while loop. 77 00:05:10,120 --> 00:05:13,390 So now coming back to our Reeborg's World, 78 00:05:13,450 --> 00:05:17,260 I want you to click on this dropdown and go to hurdle number 2. 79 00:05:17,950 --> 00:05:19,930 Now this is a little bit different. 80 00:05:20,080 --> 00:05:22,630 And if you want to see the explanation for this exercise, 81 00:05:22,960 --> 00:05:27,550 then simply just click on this 'World Info' and you'll see the explanation. 82 00:05:28,210 --> 00:05:32,380 So essentially this hurdle's race is a little bit different from the last one. 83 00:05:33,010 --> 00:05:34,000 Whereas the last one 84 00:05:34,000 --> 00:05:38,890 we knew that we had to always complete six hurdles to get to the goalpost. 85 00:05:39,190 --> 00:05:43,570 In this case, we no longer know where the flag is going to be. 86 00:05:43,570 --> 00:05:47,080 So it could be here, it could be here, it could be here, et cetera. 87 00:05:47,290 --> 00:05:50,890 And it's going to be set randomly to one of these positions. 88 00:05:51,460 --> 00:05:55,780 But what we now get is some sort of condition called at_goal. 89 00:05:56,470 --> 00:06:01,470 And if this at_goal is true then it means our robot has landed on a flag. 90 00:06:03,320 --> 00:06:05,540 But if this at_goal is not true, 91 00:06:06,020 --> 00:06:09,320 then it means that it's not yet reached the flag. 92 00:06:09,860 --> 00:06:14,860 So we can use our knowledge of while loops as well as this condition to use our 93 00:06:15,110 --> 00:06:20,110 code that we had from previously to get our robot to complete this hurdle race. 94 00:06:21,260 --> 00:06:24,830 So you should still have the code from the previous challenge up 95 00:06:25,220 --> 00:06:30,220 and all you have to do is to think about how you would create a while loop so 96 00:06:31,190 --> 00:06:36,190 that when you run this program and the flag is set randomly... 97 00:06:36,890 --> 00:06:41,030 So this time it was set to be here, but the next time I run it, 98 00:06:41,120 --> 00:06:45,380 you can see it's now set here. It doesn't matter where the flag is, 99 00:06:45,500 --> 00:06:49,700 your robot should be able to detect if it's at flag, if its at the goal, 100 00:06:50,000 --> 00:06:53,030 and if it's not then it should continue jumping. 101 00:06:53,720 --> 00:06:58,280 Pause the video quickly and see if you can complete this challenge and then come 102 00:06:58,280 --> 00:07:00,440 back and we'll walk through the solution together. 103 00:07:03,250 --> 00:07:03,980 Right? 104 00:07:03,980 --> 00:07:04,300 All right. 105 00:07:04,300 --> 00:07:08,650 So first things first, we know that we're going to need to use a while loop and 106 00:07:08,650 --> 00:07:13,650 the condition that we're going to be testing for is this one, at_goal. 107 00:07:14,230 --> 00:07:17,800 This condition can be true or it can be false. 108 00:07:18,490 --> 00:07:23,490 And basically we're looking to see while at_goal is not equal to true, 109 00:07:27,940 --> 00:07:30,790 so while our robot is not at the goal, 110 00:07:31,150 --> 00:07:33,970 then we want to perform the jump function. 111 00:07:34,630 --> 00:07:39,630 So we're going to jump for as many times as it is required until this at_goal 112 00:07:40,810 --> 00:07:41,830 becomes true. 113 00:07:42,850 --> 00:07:47,850 Another way of expressing this at_goal is not true is to say while not at_goal. 114 00:07:52,690 --> 00:07:56,980 So this is what they mean by the negation of this condition. 115 00:07:58,030 --> 00:08:01,900 Either way of testing this will work. And now if I hit run, 116 00:08:01,930 --> 00:08:05,500 you can see that even though we only need to perform one hurdle, 117 00:08:05,800 --> 00:08:10,800 as soon as our robot gets here and it checks that at_goal is true then it will 118 00:08:12,340 --> 00:08:14,560 stop this loop. 119 00:08:15,610 --> 00:08:19,660 But if our loop goes on for much longer, say the goal is over here, 120 00:08:20,170 --> 00:08:24,040 then once it reaches here and at_goal is still false, 121 00:08:24,580 --> 00:08:29,230 then it's going to continue jumping until at_goal becomes true. 122 00:08:30,250 --> 00:08:32,919 So that required a little bit of mental jujitsu 123 00:08:32,919 --> 00:08:36,669 I think because this entire condition has to be true, 124 00:08:37,210 --> 00:08:42,210 but this at_goal is actually going to be false until the moment where we 125 00:08:42,730 --> 00:08:45,580 reached the goal. So by adding this not 126 00:08:45,610 --> 00:08:49,390 we effectively flip this at_goal. So if it was true, 127 00:08:49,390 --> 00:08:54,390 becomes false or if it's false becomes true in order to continue jumping while 128 00:08:54,910 --> 00:08:56,100 we're not at the goal. 129 00:08:56,610 --> 00:09:01,610 I prefer this particular type of syntax because it reads more like English: while 130 00:09:01,830 --> 00:09:04,770 not at goal then perform jump. 131 00:09:05,220 --> 00:09:09,420 And if it is at_goal then it will stop. 132 00:09:10,680 --> 00:09:13,560 And one of the things you might be wondering right now is, 133 00:09:13,650 --> 00:09:17,310 so I've learned about the for loop and I've learned about a while loop. 134 00:09:17,880 --> 00:09:22,050 And if I can use both of them, why would I choose one over the other? 135 00:09:22,110 --> 00:09:26,640 When would I use a for loop? And when would I use a while loop? Well, 136 00:09:26,700 --> 00:09:30,480 what I would tend to say is that for loops are really great 137 00:09:30,480 --> 00:09:35,480 when you want to iterate over something and you need to do something with each 138 00:09:35,580 --> 00:09:40,320 thing that you are iterating over. So for example, 139 00:09:40,320 --> 00:09:45,320 if you're iterating through a list and you're saying for each fruit in our list 140 00:09:45,780 --> 00:09:50,670 of fruits and you want to be able to say, I don't know, 141 00:09:50,730 --> 00:09:55,110 do something with each of these items in here. For instance, 142 00:09:55,320 --> 00:09:59,010 it can be as simple as just printing it. Well, 143 00:09:59,010 --> 00:10:01,320 then this will require a for loop. 144 00:10:01,470 --> 00:10:05,130 You can't do this very easily using a while loop. Now, 145 00:10:05,130 --> 00:10:08,520 similarly we've used the range function, right? 146 00:10:08,520 --> 00:10:13,520 So for range from one to six print each number in the range. 147 00:10:15,420 --> 00:10:18,330 Well this it's also easy to do with a for loop. 148 00:10:19,050 --> 00:10:23,850 Now you want to be using a while loop when you don't really care what number in a 149 00:10:23,850 --> 00:10:24,450 sequence you're 150 00:10:24,450 --> 00:10:29,450 in, which item you're iterating through in a list and you just simply want to carry 151 00:10:30,270 --> 00:10:32,760 out some sort of functionality many, 152 00:10:32,760 --> 00:10:36,690 many times until some sort of condition that you set. 153 00:10:37,740 --> 00:10:42,240 And this is also a good point to mention that while loops are a little bit more 154 00:10:42,240 --> 00:10:46,710 dangerous than for loops because in for loops, you're setting ahead of time 155 00:10:46,740 --> 00:10:48,750 how many times something is going to run. 156 00:10:49,110 --> 00:10:52,650 It's going to stop once it reaches the end of the list of items in this case, 157 00:10:52,950 --> 00:10:57,120 and it's going to stop once it reaches the upper bound of the range in this 158 00:10:57,120 --> 00:10:58,950 case. But for while loops, 159 00:10:58,980 --> 00:11:03,980 they will continue running until this particular condition switches to false. 160 00:11:05,670 --> 00:11:10,230 So if you have some sort of condition that actually never becomes false, well 161 00:11:10,230 --> 00:11:15,230 then your while loop becomes something known as an infinite loop. Because let's 162 00:11:15,840 --> 00:11:20,490 say that our while loop tested while five is greater than three, 163 00:11:20,850 --> 00:11:23,730 then carry out these three lines of code. Well, 164 00:11:23,760 --> 00:11:28,760 five is always going to be larger than three until the end of time. 165 00:11:29,580 --> 00:11:34,470 And so that means your code is also going to run until the end of time, 166 00:11:34,710 --> 00:11:39,330 which is probably not what you want in most cases. If 167 00:11:39,330 --> 00:11:43,320 instead of saying, while not at_goal, I said, while five is greater than three, 168 00:11:43,680 --> 00:11:48,360 then you're going to see this robot perform this jump until eternity. 169 00:11:48,450 --> 00:11:53,450 And it's basically going to stop only once it's crashed and timed out. 170 00:11:55,090 --> 00:12:00,090 Now every single program at some point in their lives will create an infinite 171 00:12:00,460 --> 00:12:03,790 loop. Don't worry about it. Just quit the program, 172 00:12:03,850 --> 00:12:08,050 restart and try to prevent this from happening in the future. 173 00:12:10,240 --> 00:12:15,240 And very often I find that it's really helpful when you don't know why you're 174 00:12:15,250 --> 00:12:19,210 getting an infinite loop to simply just print out your condition. 175 00:12:19,750 --> 00:12:23,350 So in this case, if I printed out five greater than three, 176 00:12:23,590 --> 00:12:26,170 then it's always going to print true. 177 00:12:27,820 --> 00:12:32,680 And it's never going to become false basically. In the next lesson, 178 00:12:32,740 --> 00:12:36,880 I've got more exercises coming up for you, namely hurdles 3 and 4. 179 00:12:37,240 --> 00:12:41,320 So head over there and put what you've learned about while loops into practice.