WEBVTT

0
00:00.430 --> 00:02.080
All right guys, in this lesson

1
00:02.110 --> 00:07.110
I want to talk about the while loop, the loop that will continue going while a

2
00:07.810 --> 00:09.640
particular condition is true.

3
00:10.660 --> 00:15.100
This is a really simple version of a robot that's using a while loop.

4
00:15.520 --> 00:16.810
So in this case,

5
00:16.990 --> 00:21.580
the instruction would be while you are plugged into the wall and you have

6
00:21.580 --> 00:26.050
electricity move forwards. So this is what happens.

7
00:26.260 --> 00:31.260
It will stop once it pulls the socket out of the wall and it stops receiving

8
00:31.720 --> 00:34.990
electricity. Let's compare this against the

9
00:34.990 --> 00:39.190
for loop that we've seen before. So we've seen sort of two flavors of the

10
00:39.190 --> 00:43.660
for loop: one where we're looping through a list of items to do something with

11
00:43.690 --> 00:45.070
each item in the list,

12
00:45.490 --> 00:50.490
and the other is using a range function where we create a range between a and b

13
00:51.160 --> 00:55.990
and then we use every number in that range to do something. For example,

14
00:55.990 --> 01:00.610
in this case would be to print it out. The while loop looks

15
01:00.640 --> 01:03.520
something like this. And in this case,

16
01:03.880 --> 01:07.810
while this particular condition is true,

17
01:08.440 --> 01:12.400
then we go inside the loop and we do something repeatedly.

18
01:12.910 --> 01:17.290
And it's only when the something becomes false does the loop stop.

19
01:18.610 --> 01:22.120
So let's take a look at what the code would look like in real life.

20
01:22.570 --> 01:27.250
So here I have the Hurdle 1 challenge that you did previously.

21
01:27.850 --> 01:28.750
And for this

22
01:28.810 --> 01:33.810
we created a for loop in order to get our robot to jump over the hurdle six

23
01:34.450 --> 01:36.970
times to get to the final destination.

24
01:37.630 --> 01:42.010
Now we could also do the same thing, but using a while loop.

25
01:42.280 --> 01:43.540
And this is how we would do it.

26
01:43.930 --> 01:48.310
Let's say we create a variable called a number_of_hurdles,

27
01:48.730 --> 01:52.120
and we set that equal to six because there are six hurdles.

28
01:52.690 --> 01:57.690
And then we use a while loop to loop through these hurdles until this goes down

29
01:59.140 --> 02:02.800
to zero. So we can say while number_of_hurdles

30
02:02.850 --> 02:03.683
<v 1>Right,</v>

31
02:07.100 --> 02:11.390
<v 0>> 0:</v>

32
02:12.050 --> 02:14.360
and then we get it to do something. Well in this case,

33
02:14.360 --> 02:17.900
what we need it to do is to perform the jump function.

34
02:18.860 --> 02:23.150
And at the end of the loop, just before we go back to the beginning,

35
02:23.270 --> 02:26.270
we're going to decrease the number of hurdles,

36
02:26.390 --> 02:27.223
<v 1>Right</v>

37
02:28.700 --> 02:31.040
<v 0>by one., -= 1.</v>

38
02:31.730 --> 02:36.730
So now let's go ahead and delete this version using the for loop. And in order to

39
02:37.610 --> 02:39.350
better visualize what's happening here

40
02:39.350 --> 02:44.120
I'm actually also going to add a print statement to print that variable.

41
02:44.700 --> 02:45.533
<v 1>Right?</v>

42
02:47.930 --> 02:49.880
<v 0>So now let's step through this.</v>

43
02:50.300 --> 02:55.300
And the first time we enter the while loop, number_of_hurdles equals six,

44
02:55.430 --> 02:56.900
six is greater than zero.

45
02:57.200 --> 03:01.900
Then that is a go ahead to jump into the loop and execute these three lines of

46
03:01.900 --> 03:06.730
code. So now we go in and it performs the jump function.

47
03:09.430 --> 03:13.090
And then once that's done, it comes back, goes onto the next line,

48
03:13.450 --> 03:17.620
number_of_hurdles minus equals one, so six should become five.

49
03:17.860 --> 03:22.750
So now when it executes this print function, we should see five printed.

50
03:23.530 --> 03:28.270
So lets move this little pop up away on the side and let's keep going.

51
03:28.270 --> 03:31.900
So now it's gone back to the beginning of the while loop and it's testing this

52
03:31.900 --> 03:36.900
condition once more: is the number of hurdles, five, still greater than zero.

53
03:37.330 --> 03:38.770
Well, so if it's true,

54
03:38.860 --> 03:43.860
then it goes into the while loop again and performs all of those lines of code.

55
03:45.250 --> 03:49.000
So now it's minus one and it should now be four.

56
03:49.720 --> 03:54.720
And it keeps on going until the point where this condition number_of_hurdles

57
03:56.140 --> 03:59.020
greater than zero becomes false.

58
04:00.280 --> 04:05.280
So now this is the moment where we're going to subtract a further one from our

59
04:05.980 --> 04:08.650
number of hurdles, which is currently one.

60
04:08.980 --> 04:12.760
So that should take it down to zero. And once that's done,

61
04:13.120 --> 04:17.710
this condition is no longer true. Zero is not greater than zero.

62
04:18.040 --> 04:20.530
Zero is equal to zero, but it's not greater.

63
04:20.980 --> 04:25.980
So now this is false and we exit out of the while loop and we end our program.

64
04:27.700 --> 04:31.000
This is what the syntax for the while loop looks like. First,

65
04:31.030 --> 04:34.960
we have the while keyword and then we have some sort of condition that we're

66
04:34.960 --> 04:35.860
going to test.

67
04:36.130 --> 04:40.660
So previously you saw it was number of hurdles being greater than zero,

68
04:41.110 --> 04:43.240
and whenever that condition is true,

69
04:43.540 --> 04:48.070
then it's going to look inside the while loop at the indented lines of code to

70
04:48.070 --> 04:52.420
carry out those instructions, to do this, then do this, then do this.

71
04:52.720 --> 04:55.120
And finally, when it gets to the end of the while loop,

72
04:55.150 --> 04:59.080
it comes back to the beginning and tests this condition again.

73
04:59.230 --> 05:00.370
And if it's still true,

74
05:00.640 --> 05:05.230
then it's going to go through and loop and loop and loop until this condition

75
05:05.230 --> 05:09.310
becomes false at which point it ends an exits the while loop.

76
05:10.120 --> 05:13.390
So now coming back to our Reeborg's World,

77
05:13.450 --> 05:17.260
I want you to click on this dropdown and go to hurdle number 2.

78
05:17.950 --> 05:19.930
Now this is a little bit different.

79
05:20.080 --> 05:22.630
And if you want to see the explanation for this exercise,

80
05:22.960 --> 05:27.550
then simply just click on this 'World Info' and you'll see the explanation.

81
05:28.210 --> 05:32.380
So essentially this hurdle's race is a little bit different from the last one.

82
05:33.010 --> 05:34.000
Whereas the last one

83
05:34.000 --> 05:38.890
we knew that we had to always complete six hurdles to get to the goalpost.

84
05:39.190 --> 05:43.570
In this case, we no longer know where the flag is going to be.

85
05:43.570 --> 05:47.080
So it could be here, it could be here, it could be here, et cetera.

86
05:47.290 --> 05:50.890
And it's going to be set randomly to one of these positions.

87
05:51.460 --> 05:55.780
But what we now get is some sort of condition called at_goal.

88
05:56.470 --> 06:01.470
And if this at_goal is true then it means our robot has landed on a flag.

89
06:03.320 --> 06:05.540
But if this at_goal is not true,

90
06:06.020 --> 06:09.320
then it means that it's not yet reached the flag.

91
06:09.860 --> 06:14.860
So we can use our knowledge of while loops as well as this condition to use our

92
06:15.110 --> 06:20.110
code that we had from previously to get our robot to complete this hurdle race.

93
06:21.260 --> 06:24.830
So you should still have the code from the previous challenge up

94
06:25.220 --> 06:30.220
and all you have to do is to think about how you would create a while loop so

95
06:31.190 --> 06:36.190
that when you run this program and the flag is set randomly...

96
06:36.890 --> 06:41.030
So this time it was set to be here, but the next time I run it,

97
06:41.120 --> 06:45.380
you can see it's now set here. It doesn't matter where the flag is,

98
06:45.500 --> 06:49.700
your robot should be able to detect if it's at flag, if its at the goal,

99
06:50.000 --> 06:53.030
and if it's not then it should continue jumping.

100
06:53.720 --> 06:58.280
Pause the video quickly and see if you can complete this challenge and then come

101
06:58.280 --> 07:00.440
back and we'll walk through the solution together.

102
07:03.250 --> 07:03.980
<v 2>Right?</v>

103
07:03.980 --> 07:04.300
<v 0>All right.</v>

104
07:04.300 --> 07:08.650
So first things first, we know that we're going to need to use a while loop and

105
07:08.650 --> 07:13.650
the condition that we're going to be testing for is this one, at_goal.

106
07:14.230 --> 07:17.800
This condition can be true or it can be false.

107
07:18.490 --> 07:23.490
And basically we're looking to see while at_goal is not equal to true,

108
07:27.940 --> 07:30.790
so while our robot is not at the goal,

109
07:31.150 --> 07:33.970
then we want to perform the jump function.

110
07:34.630 --> 07:39.630
So we're going to jump for as many times as it is required until this at_goal

111
07:40.810 --> 07:41.830
becomes true.

112
07:42.850 --> 07:47.850
Another way of expressing this at_goal is not true is to say while not at_goal.

113
07:52.690 --> 07:56.980
So this is what they mean by the negation of this condition.

114
07:58.030 --> 08:01.900
Either way of testing this will work. And now if I hit run,

115
08:01.930 --> 08:05.500
you can see that even though we only need to perform one hurdle,

116
08:05.800 --> 08:10.800
as soon as our robot gets here and it checks that at_goal is true then it will

117
08:12.340 --> 08:14.560
stop this loop.

118
08:15.610 --> 08:19.660
But if our loop goes on for much longer, say the goal is over here,

119
08:20.170 --> 08:24.040
then once it reaches here and at_goal is still false,

120
08:24.580 --> 08:29.230
then it's going to continue jumping until at_goal becomes true.

121
08:30.250 --> 08:32.920
So that required a little bit of mental jujitsu

122
08:32.920 --> 08:36.670
I think because this entire condition has to be true,

123
08:37.210 --> 08:42.210
but this at_goal is actually going to be false until the moment where we

124
08:42.730 --> 08:45.580
reached the goal. So by adding this not

125
08:45.610 --> 08:49.390
we effectively flip this at_goal. So if it was true,

126
08:49.390 --> 08:54.390
becomes false or if it's false becomes true in order to continue jumping while

127
08:54.910 --> 08:56.100
we're not at the goal.

128
08:56.610 --> 09:01.610
I prefer this particular type of syntax because it reads more like English: while

129
09:01.830 --> 09:04.770
not at goal then perform jump.

130
09:05.220 --> 09:09.420
And if it is at_goal then it will stop.

131
09:10.680 --> 09:13.560
And one of the things you might be wondering right now is,

132
09:13.650 --> 09:17.310
so I've learned about the for loop and I've learned about a while loop.

133
09:17.880 --> 09:22.050
And if I can use both of them, why would I choose one over the other?

134
09:22.110 --> 09:26.640
When would I use a for loop? And when would I use a while loop? Well,

135
09:26.700 --> 09:30.480
what I would tend to say is that for loops are really great

136
09:30.480 --> 09:35.480
when you want to iterate over something and you need to do something with each

137
09:35.580 --> 09:40.320
thing that you are iterating over. So for example,

138
09:40.320 --> 09:45.320
if you're iterating through a list and you're saying for each fruit in our list

139
09:45.780 --> 09:50.670
of fruits and you want to be able to say, I don't know,

140
09:50.730 --> 09:55.110
do something with each of these items in here. For instance,

141
09:55.320 --> 09:59.010
it can be as simple as just printing it. Well,

142
09:59.010 --> 10:01.320
then this will require a for loop.

143
10:01.470 --> 10:05.130
You can't do this very easily using a while loop. Now,

144
10:05.130 --> 10:08.520
similarly we've used the range function, right?

145
10:08.520 --> 10:13.520
So for range from one to six print each number in the range.

146
10:15.420 --> 10:18.330
Well this it's also easy to do with a for loop.

147
10:19.050 --> 10:23.850
Now you want to be using a while loop when you don't really care what number in a

148
10:23.850 --> 10:24.450
sequence you're

149
10:24.450 --> 10:29.450
in, which item you're iterating through in a list and you just simply want to carry

150
10:30.270 --> 10:32.760
out some sort of functionality many,

151
10:32.760 --> 10:36.690
many times until some sort of condition that you set.

152
10:37.740 --> 10:42.240
And this is also a good point to mention that while loops are a little bit more

153
10:42.240 --> 10:46.710
dangerous than for loops because in for loops, you're setting ahead of time

154
10:46.740 --> 10:48.750
how many times something is going to run.

155
10:49.110 --> 10:52.650
It's going to stop once it reaches the end of the list of items in this case,

156
10:52.950 --> 10:57.120
and it's going to stop once it reaches the upper bound of the range in this

157
10:57.120 --> 10:58.950
case. But for while loops,

158
10:58.980 --> 11:03.980
they will continue running until this particular condition switches to false.

159
11:05.670 --> 11:10.230
So if you have some sort of condition that actually never becomes false, well

160
11:10.230 --> 11:15.230
then your while loop becomes something known as an infinite loop. Because let's

161
11:15.840 --> 11:20.490
say that our while loop tested while five is greater than three,

162
11:20.850 --> 11:23.730
then carry out these three lines of code. Well,

163
11:23.760 --> 11:28.760
five is always going to be larger than three until the end of time.

164
11:29.580 --> 11:34.470
And so that means your code is also going to run until the end of time,

165
11:34.710 --> 11:39.330
which is probably not what you want in most cases. If

166
11:39.330 --> 11:43.320
instead of saying, while not at_goal, I said, while five is greater than three,

167
11:43.680 --> 11:48.360
then you're going to see this robot perform this jump until eternity.

168
11:48.450 --> 11:53.450
And it's basically going to stop only once it's crashed and timed out.

169
11:55.090 --> 12:00.090
Now every single program at some point in their lives will create an infinite

170
12:00.460 --> 12:03.790
loop. Don't worry about it. Just quit the program,

171
12:03.850 --> 12:08.050
restart and try to prevent this from happening in the future.

172
12:10.240 --> 12:15.240
And very often I find that it's really helpful when you don't know why you're

173
12:15.250 --> 12:19.210
getting an infinite loop to simply just print out your condition.

174
12:19.750 --> 12:23.350
So in this case, if I printed out five greater than three,

175
12:23.590 --> 12:26.170
then it's always going to print true.

176
12:27.820 --> 12:32.680
And it's never going to become false basically. In the next lesson,

177
12:32.740 --> 12:36.880
I've got more exercises coming up for you, namely hurdles 3 and 4.

178
12:37.240 --> 12:41.320
So head over there and put what you've learned about while loops into practice.