All right guys, in this lesson
I want to talk about the while loop, the loop that will continue going while a
particular condition is true.
This is a really simple version of a robot that's using a while loop.
So in this case,
the instruction would be while you are plugged into the wall and you have
electricity move forwards. So this is what happens.
It will stop once it pulls the socket out of the wall and it stops receiving
electricity. Let's compare this against the
for loop that we've seen before. So we've seen sort of two flavors of the
for loop: one where we're looping through a list of items to do something with
each item in the list,
and the other is using a range function where we create a range between a and b
and then we use every number in that range to do something. For example,
in this case would be to print it out. The while loop looks
something like this. And in this case,
while this particular condition is true,
then we go inside the loop and we do something repeatedly.
And it's only when the something becomes false does the loop stop.
So let's take a look at what the code would look like in real life.
So here I have the Hurdle 1 challenge that you did previously.
And for this
we created a for loop in order to get our robot to jump over the hurdle six
times to get to the final destination.
Now we could also do the same thing, but using a while loop.
And this is how we would do it.
Let's say we create a variable called a number_of_hurdles,
and we set that equal to six because there are six hurdles.
And then we use a while loop to loop through these hurdles until this goes down
to zero. So we can say while number_of_hurdles
1: Right,
0: > 0:
and then we get it to do something. Well in this case,
what we need it to do is to perform the jump function.
And at the end of the loop, just before we go back to the beginning,
we're going to decrease the number of hurdles,
1: Right
0: by one., -= 1.
So now let's go ahead and delete this version using the for loop. And in order to
better visualize what's happening here
I'm actually also going to add a print statement to print that variable.
1: Right?
0: So now let's step through this.
And the first time we enter the while loop, number_of_hurdles equals six,
six is greater than zero.
Then that is a go ahead to jump into the loop and execute these three lines of
code. So now we go in and it performs the jump function.
And then once that's done, it comes back, goes onto the next line,
number_of_hurdles minus equals one, so six should become five.
So now when it executes this print function, we should see five printed.
So lets move this little pop up away on the side and let's keep going.
So now it's gone back to the beginning of the while loop and it's testing this
condition once more: is the number of hurdles, five, still greater than zero.
Well, so if it's true,
then it goes into the while loop again and performs all of those lines of code.
So now it's minus one and it should now be four.
And it keeps on going until the point where this condition number_of_hurdles
greater than zero becomes false.
So now this is the moment where we're going to subtract a further one from our
number of hurdles, which is currently one.
So that should take it down to zero. And once that's done,
this condition is no longer true. Zero is not greater than zero.
Zero is equal to zero, but it's not greater.
So now this is false and we exit out of the while loop and we end our program.
This is what the syntax for the while loop looks like. First,
we have the while keyword and then we have some sort of condition that we're
going to test.
So previously you saw it was number of hurdles being greater than zero,
and whenever that condition is true,
then it's going to look inside the while loop at the indented lines of code to
carry out those instructions, to do this, then do this, then do this.
And finally, when it gets to the end of the while loop,
it comes back to the beginning and tests this condition again.
And if it's still true,
then it's going to go through and loop and loop and loop until this condition
becomes false at which point it ends an exits the while loop.
So now coming back to our Reeborg's World,
I want you to click on this dropdown and go to hurdle number 2.
Now this is a little bit different.
And if you want to see the explanation for this exercise,
then simply just click on this 'World Info' and you'll see the explanation.
So essentially this hurdle's race is a little bit different from the last one.
Whereas the last one
we knew that we had to always complete six hurdles to get to the goalpost.
In this case, we no longer know where the flag is going to be.
So it could be here, it could be here, it could be here, et cetera.
And it's going to be set randomly to one of these positions.
But what we now get is some sort of condition called at_goal.
And if this at_goal is true then it means our robot has landed on a flag.
But if this at_goal is not true,
then it means that it's not yet reached the flag.
So we can use our knowledge of while loops as well as this condition to use our
code that we had from previously to get our robot to complete this hurdle race.
So you should still have the code from the previous challenge up
and all you have to do is to think about how you would create a while loop so
that when you run this program and the flag is set randomly...
So this time it was set to be here, but the next time I run it,
you can see it's now set here. It doesn't matter where the flag is,
your robot should be able to detect if it's at flag, if its at the goal,
and if it's not then it should continue jumping.
Pause the video quickly and see if you can complete this challenge and then come
back and we'll walk through the solution together.
2: Right?
0: All right.
So first things first, we know that we're going to need to use a while loop and
the condition that we're going to be testing for is this one, at_goal.
This condition can be true or it can be false.
And basically we're looking to see while at_goal is not equal to true,
so while our robot is not at the goal,
then we want to perform the jump function.
So we're going to jump for as many times as it is required until this at_goal
becomes true.
Another way of expressing this at_goal is not true is to say while not at_goal.
So this is what they mean by the negation of this condition.
Either way of testing this will work. And now if I hit run,
you can see that even though we only need to perform one hurdle,
as soon as our robot gets here and it checks that at_goal is true then it will
stop this loop.
But if our loop goes on for much longer, say the goal is over here,
then once it reaches here and at_goal is still false,
then it's going to continue jumping until at_goal becomes true.
So that required a little bit of mental jujitsu
I think because this entire condition has to be true,
but this at_goal is actually going to be false until the moment where we
reached the goal. So by adding this not
we effectively flip this at_goal. So if it was true,
becomes false or if it's false becomes true in order to continue jumping while
we're not at the goal.
I prefer this particular type of syntax because it reads more like English: while
not at goal then perform jump.
And if it is at_goal then it will stop.
And one of the things you might be wondering right now is,
so I've learned about the for loop and I've learned about a while loop.
And if I can use both of them, why would I choose one over the other?
When would I use a for loop? And when would I use a while loop? Well,
what I would tend to say is that for loops are really great
when you want to iterate over something and you need to do something with each
thing that you are iterating over. So for example,
if you're iterating through a list and you're saying for each fruit in our list
of fruits and you want to be able to say, I don't know,
do something with each of these items in here. For instance,
it can be as simple as just printing it. Well,
then this will require a for loop.
You can't do this very easily using a while loop. Now,
similarly we've used the range function, right?
So for range from one to six print each number in the range.
Well this it's also easy to do with a for loop.
Now you want to be using a while loop when you don't really care what number in a
sequence you're
in, which item you're iterating through in a list and you just simply want to carry
out some sort of functionality many,
many times until some sort of condition that you set.
And this is also a good point to mention that while loops are a little bit more
dangerous than for loops because in for loops, you're setting ahead of time
how many times something is going to run.
It's going to stop once it reaches the end of the list of items in this case,
and it's going to stop once it reaches the upper bound of the range in this
case. But for while loops,
they will continue running until this particular condition switches to false.
So if you have some sort of condition that actually never becomes false, well
then your while loop becomes something known as an infinite loop. Because let's
say that our while loop tested while five is greater than three,
then carry out these three lines of code. Well,
five is always going to be larger than three until the end of time.
And so that means your code is also going to run until the end of time,
which is probably not what you want in most cases. If
instead of saying, while not at_goal, I said, while five is greater than three,
then you're going to see this robot perform this jump until eternity.
And it's basically going to stop only once it's crashed and timed out.
Now every single program at some point in their lives will create an infinite
loop. Don't worry about it. Just quit the program,
restart and try to prevent this from happening in the future.
And very often I find that it's really helpful when you don't know why you're
getting an infinite loop to simply just print out your condition.
So in this case, if I printed out five greater than three,
then it's always going to print true.
And it's never going to become false basically. In the next lesson,
I've got more exercises coming up for you, namely hurdles 3 and 4.
So head over there and put what you've learned about while loops into practice.