-
INSTRUCTOR: Hello, everybody.
-
Today we're going to be taking
a look at While loops in Python.
-
The While loop in Python is used
to iterate over a block of code
-
as long as the test
condition is true.
-
Now, the difference between
a For loop and a While loop
-
is that a For loop
is going to iterate
-
over the entire sequence,
regardless of a condition,
-
but the While loop is only going
to iterate over that sequence as
-
long as a specific
condition is met.
-
Once that condition is not
met, the code is going to stop.
-
And it's not going to
iterate through the rest
-
of the sequence.
-
So if we take a look at
this flowchart right here,
-
we're going to enter
this While loop.
-
And we have a test
condition right here.
-
The first time that this test
condition comes back false,
-
it's going to exit
the While loop.
-
So let's start actually
writing out the code
-
and see how this
While loop works.
-
So let's create a variable.
-
We're just going to say,
number is equal to 1.
-
And then we'll say While.
-
And now we need to
write our condition that
-
needs to be met in order
for our block of code
-
beneath this to run.
-
So we're going to say,
While number is less than 5,
-
and then we'll do colon, Enter.
-
And now this is
our block of code.
-
We're going to say Print.
-
And then we'll say, number.
-
Now what we need to do is
basically create a counter.
-
We're going to say, number
equals number plus 1.
-
If you've never done something
like this, it's like a counter.
-
Most people start it at 0.
-
In fact, let's start it at 0.
-
And then each time it runs
through this While loop,
-
it's going to add 1 to
this number up here.
-
And then it's going to
become a 1, a 2, a 3 each
-
time it iterates
through this While loop.
-
Now, once this number is
no longer less than 5,
-
it'll break out
of the While loop.
-
And it will no longer run.
-
So let's run this really
quick by hitting Shift-Enter.
-
So it starts at 0.
-
And it's going to say, While
the number is less than 5,
-
Print number.
-
So the first time that
it runs through, it is 0.
-
And so it prints 0.
-
And then it adds 1 to Number.
-
And then it continues that
While loop right here.
-
And it keeps looping
through this portion.
-
It never goes back up
here to this line of code.
-
This is just our variable
that we start with.
-
And then once this condition is
no longer met, once it is false,
-
then it's going to
break out of that code.
-
Now that we basically know
how a While loop works,
-
let's look at something
called a Break statement.
-
So let's copy this
right down here.
-
And what we're going to say
is, if number is equal to 3,
-
we're going to Break.
-
Now, with the
Break statement, we
-
can basically stop the loop even
if the While condition is true.
-
So while this number
is less than 5,
-
it's going to continue
to loop through.
-
But now we have this
Break statement.
-
So it's going to say,
if the Number equals 3,
-
we're going to break
out of this While loop.
-
But if this is
false, we're going
-
to continue adding to that
number just like normal.
-
So let's execute this.
-
So as you can see, it only
went to 3 instead of 4
-
like before because
each time it was
-
running through this While
loop, it was checking
-
if the number was equal to 3.
-
And once it got to
3, this became true.
-
And then we broke out
of this While loop.
-
The next thing that I want to
look at-- and we'll copy this
-
right down here--
-
is an Else statement,
much like an If statement.
-
But we can use the Else
statement with a While loop,
-
which runs the block of code.
-
And when that condition
is no longer true,
-
then it activates
the Else statement.
-
So we'll go right down here.
-
And we'll say, Else.
-
And we'll do a colon and Enter.
-
And then, we'll say Print.
-
And we'll say, no
longer less than 5.
-
Now because this If statement is
still in there, it will break.
-
So let's say, 6.
-
And then we'll run this.
-
And so it's going to iterate
through this block of code.
-
And once this statement
is no longer true,
-
once we break out
of it, we're going
-
to go to our Else statement.
-
Now, as long as this
statement is true,
-
it's going to continue
to iterate through.
-
But once this
condition is not met,
-
then it will go to
our Else statement.
-
And we'll run that line of code.
-
Now the Else statement is
only going to trigger if the
-
While loop no longer is true.
-
If we have something
like this If statement
-
that causes it to break
out of the While loop,
-
the Else statement
will no longer work.
-
So let's say, if the number
is 3, and we run this,
-
the Else statement is no
longer going to trigger.
-
So this body of code
will not be run.
-
Now, the next thing
that I want to look at
-
is the Continue statement.
-
If the Continue
statement is triggered,
-
it basically rejects
all remaining statements
-
in the current
iteration of the loop.
-
And then we'll go to
the next iteration.
-
Now to demonstrate
this, I'm going
-
to change this Break
into a Continue.
-
So before when we had the Break,
if the number was equal to 3,
-
it would stop all
the code completely.
-
But when we change this
to Continue, which we'll
-
do right now, what
it's going to do
-
is, it's no longer
going to run through any
-
of the subsequent code
in this block of code,
-
it's just going to go
straight up to the beginning
-
and restart our While loop.
-
So what's going
to happen when we
-
run this is, it's going to come
to 3, it's going to become 3.
-
And it's going to continue
back into the While loop.
-
But it's never going to
have that number changed
-
to be add it to 1 to
continue with the While loop.
-
This will basically
create an infinite loop.
-
Let's try this really quickly.
-
And as you can see, it's
going to stay 3 forever.
-
Eventually, this would time
out, but I'm just going
-
to stop the code really quick.
-
So if we just change up
the order of which we're
-
doing things, we're
going to say, there,
-
and we're going to
put this down here.
-
So what it's going
to do now, instead
-
of printing the number
immediately and then
-
adding the number
later, we're going
-
to add the number right away.
-
And then we're going
to say, if it is 3,
-
we're going to continue.
-
And it's going to
print the number.
-
So let's try executing
this and see what happens.
-
So as you can see, we no longer
have the 3 in our output.
-
What it did was, when
we got to the number 3,
-
it continued and didn't
execute this right here,
-
which prints off that number.
-
So that really is the
basics of the While loop.
-
I hope that this was helpful.
-
I hope that you learned
something in this video.
-
If you did, be sure to
and subscribe below.
-
And I'll see you
in the next video.
-