Type the following at the Python prompt:
count = 1
while count > 9:
right()
count = count + 1
What happened? Nothing! Neko didn’t move an inch!
Why?
Simple. Remeber, `while count > 9′ means: `repeatedly perform the
actions given below as long as count is greater than 9′. Python
looks at the value of `count’; it is 1. Now, 1 is not greater than
nine, so the actions given below are not done even once!
Programmers call things like `count > 9′, ‘x < 10′ etc as
`conditions’. The lines you write after the `while’ is called the
`body’ of the loop. Before Python gets into the body of a loop,
it checks whether the condition is true - if it is true, whatever
is mentioned in the body is done and Python comes back and checks
the condition; if it is true, again Python does the actions mentioned
in the body - this process repeats. When does it stop? When Python
sees that the condition has become false!
How many times will Neko move if you type the following code (move
Neko to the top left cell before you try all the examples below):
count = 1
while count < 9:
right()
count = count + 2
Can you guess the answer before you do it on the computer?
Another example - again, find out how many times Neko will move.
Try to find the answer without typing it out - then check
whether it is correct by trying it on the computer.
count = 1
while count < 9:
right()
count = count * 2
One more example:
count = 1
while count != 9:
right()
count = count + 1
What does `count != 9′ mean? It says `count NOT EQUAL TO nine’.
So, `while count != 9′ means `as long as count is not equal to nine,
repeat the body of the loop’
The Infinite Loop
Try the following code at the Python prompt:
count = 1
while count < 9:
right()
Something is missing here - we are not typing `count = count + 1′!
What is the output that you are seeing?
Neko has moved to the rightmost cell. But you see that Python
keeps on `printing’ 0, 0, 0, … you can’t give any new commands to
Python because the prompt has disappeared … this is really
mysterious!
Not really. Look at it from Python’s point of view. What does Python
do? It checks whether count is less than 9. Yes, it is (count is
initially 1). It then performs the action given in the `body’ of
the loop - which is just one line - `right()’. Python goes to the
beginning of the loop and again checks whether count is less than
nine - it is - in fact, it is still 1. So the body is done once
again … if Python is to stop doing the loop, `count’ has to
become equal to or greater than nine. This will not happen because
we are not increasing the value of `count’ in the `body’ of the loop.
So, Python executes a NEVER ENDING, loop, a so called, `infinite loop’
- right(), right(), right(), right() ………
Why do we see a never ending stream of zero’s? We have seen that
if `right’ is not able to move Neko to the next cell, it will print
zero. Once Neko has reached the rightmost cell, doing `right’ again
and again and again will result in zeros getting printed.
Is there no way we can stop this `infinite loop’. Yes there is!
Identify the key labelled `Ctrl’ on your keyboard - it will be
the leftmost key in the bottom row. Now, keep this key pressed
and press the key labelled `c’. Keep pressing for a moment and
you will see that Python has stopped doing the infinite loop!
2 Comments
Programmers call…The lines you write after the `while’ is called the
`body’ of the loop. I think you meant “The lines you write after the “:” is called the
`body’ of the loop.”
Things are seeming to be pretty simple when you put it like this. I cant think from a 8th class point of view but I do believe it will be simple for them too..
You are not leaving any scope for a question out there.. Explaining every teeny bit of detail.. Probably a book should be written like this.. ( I am a fan of the suspense you used to create in the classes, where you will stop in the mid and we were supposed to guess things out. )
Once again Great going Sir.
Anish,
The problem precisely is this - It’s hard for any of us to think from the point of view of an 8th std student - so we don’t really know whether all this is going to be effective. Anyway - I will try to `field test’ the material shortly …
I will make the correction which you suggested - I was trying to imply `the line while count > 9:’ by justing stating `while’ - maybe, its better to be more explicit and say - the lines after `while count < 9:’ ….