In this post, I will write about While loops in Python.

If you have read earlier posts For and While Loops you will probably recognize alot of this.

Count from 0 to 9

This small script will count from 0 to 9.

i = 0
while i < 10:
    print i
    i = i + 1

What does it do?

Between while and the colon, there is a value that first is True but will later be False.

As long as the statement is True , the rest of the code will run.

The code that will be run has to be in the indented block.

The i = i + 1 adds 1 to the i value for every time it runs.

Be careful to not make an eternal loop, which is when the loop continues until you press Ctrl+C.

while True:
print “Hello World”

This loop means that the while loop will always be True and will forever print Hello World.

Leave a Reply

Your email address will not be published. Required fields are marked *