I said we didn't need any new constructs, but we are gonna introduce one
and that's gonna make writing the code we need for a search engine much more convenient
It's also gonna be very useful for lots and lots of procedures that we wanna write
and the construct we are gonna introduce is called a loop
It's a way to do things over and over again
The kind of loop that we are gonna introduce first is called the while loop
The syntax for the while loop is this
We have the key word "while", followed by a test expression
followed by a colon, and then inside we have a "block"
and the "block" is a sequence of instructions
This is very similar to the "if" statement
So as a reminder, here's what the "if" statement look like
we had an "if" followed by a test expression, followed by a colon
and then an indented "block", which is a list of statements
that executes whenever the test expression evaluates to a "True" value
With "if", the block executes either zero or one times, depending on whether the test expression is "True"
With "while", the block can execute any number of times
It keeps going as long as the test expression is "True"
So with the "if " statement, if the test expression is "True"
we go to the block, run the block and then continue
if the test expression was "False", we go right to the next expression
With a "while", we also start by checking the test expression
if it's "True", we go to the block
But now, instead of going to the next statement, after the block, we go back
we try the test expression again, if it's "True", we go back to the block
we always go back to the test expression
if it's "True", we do the block again, we go back to the test expression
If it's "True" again, we do the block again, we go back to the test expression
and we can keep going around as many times as we need as long as the test expression is "True"
we'll keep executing the block and keep trying the test expression again
At some point, maybe the test expression is "False"
Once the test expression is "False", we go to the next instruction
So this means a while loop can execute the block
either zero times if the test expression was "False" at the beginning
one time if it was "True" the first time, but "False" after that
two times, three times...any number of times, it could keep going forever
There's no requirement that guarantees the test expression eventually becomes "False"
So here's an example of while loop.
We start by initializing variable i, and we'll give it a value, zero
then we have the "while", the test expression says i is less than ten
So that means as long as this evaluates to "True", we'll evaluate the block
and what the block does is print i and then add one to i
So here's what happens when this executes
So initially, the value of i is zero, i is less than ten
So that means we'll enter the loop
So that means the test expression is "True", so we'll enter the block, we'll print i
So we'll see the value zero printed
and then we'll do the assignment, so that will change the value of i
We add one to i, so that's gonna be making the value of i now refers to one
So if it was an "if", we would be done now, but because it's a "while", we keep going
We go back, test again, if i is less than ten
Now the value of i is one, which also is less than ten
so we continue, go to the block again
we are gonna print i, this time we'll see the value one
Then we go to the next statement, increase the value of i by one
that's gonna make the value of i two
Now i refers to the number two
Because it's a "while", we keep going, we go back to the test expression
i is less than ten, still less than ten, now it's two, we are gonna print the two
we are gonna add one that it will make the value of i three
and we are gonna keep going
Test again, and i is still less than ten, so we are gonna print i again
we will print the value nine, then we add one, so that's gonna have one to nine, we'll get ten
So that's the new value of i
and we go back again
and now we do i is less than ten, and now i has the value ten
Ten is not less than ten, so that will be "False" and we are done with the while loop
We'll continue with whatever statements here, in this case, there is none
So then the loopÃ¢Â€Â”Ã¢Â€Â”what we've done, we've gone through it ten times
we've printed the numbers from zero to nine
The new value of i will be ten, if we do anything here that uses the value of i
we'll see that the value of i is ten
So to see that you understand while loops, we'll have a quiz
So the question is what does this program do
Here's the program
We start by assigning zero to i
We have a while loop, where the test is not equal to ten
So the test is i is not equal to ten
Then we have i equals i plus one, so we are assigning to i the value of i plus one
and then we are printing i
So this is similar to the example, but different in a couple of ways
So it's up to you to see if you can figure out what the program does
Try to figure out yourself
You can certainly also try running this in the Python interpreter
The choices are
produce an error
print out the numbers from zero to nine
print out the numbers from one to nine
print out the numbers from one to ten
or the final choice is it runs forever or at least until our machine runs out of power
So see if you can figure out what it does
You can definitely try running it
but try to figure out on your own before running it in the Python interpreter