Friday 10 August 2012

The While Loop in Python


Loops - The While Loop


There a several types of Loop. The While loop here is very similar to that used in many other programming languages. The For loop is somewhat different.
Whats this used for?

Well occasionally I get lazy and don't want to write or type out things every time. Thats where the loop comes in.

While loops are often used when you are not quite sure how many times you want to do something. I want to say hello Philip until something happens. 
Sometimes this can be just counting using a Counter in this case

What does the counter do

I put something in a box. I take it out of the box add 1 to it and put it back in.

If this is in a loop then each time I do the loop this number will get bigger. If I want to I can count in 2's by making the counter add two each time.


Here is the code to copy in 

count =0
while (count <20):
      print "the counter = ", count
      count = count + 2
print "End of prgram"

No comments:

Post a Comment