While Loops

While loops are one of the most commonly used parts of Python and are very flexible.

While Loops

Programming is rather boring until we can do some simple loops. "While" loops will loop over the same code again and again as long as a condition you provide is true. Try the following code:

while Count<10:

You should see the numbers 0 through 9 appear in the Debug I/O panel. Try putting some calculations into the print statement and see how you can create a limitless variety of sequences of numbers.

Note: Python is very picky about indentation. In the "while" loop above we had to move in one indent for the code that is "inside" the loop. This is how Python "scopes" code to be part of a loop.

Try setting a break point in your program and then single-step through a "while" loop. What happens when you reach the end of the loop? What happens when the counter is equal to the value you specified in the "while"?

Additional Resources

An Informal Introduction to Python up to section 3.1.2

Beginner’s Guide to Python

Python for Non Programmers

© Copyright 2018 HSU - All rights reserved.