While Loops

Overview

 These repeat until a set criteria is met. Or until the loop is broken by a break keyword.

While loop keywords

while - this should by followed by a condition that while true the program will continue looping.
continue - This can be included in the block,when the program reaches this it will start a new loop iteration
break - This will terminate the loop
else - This term can be included at the end of a loop. this will run once the loop condition has been broken. This will not run if the loop exits via a break. 

Example - Basic Loop

This is a basic while loop that exists while a variable , i, is below 100. The loop then increases i, then prints i. Once i == 100 is true then the loop stops.
i = 0
while i < 100:
    i += 1
    print i
Returns:
1
2
...
99
As a programmer you should be aware of when a loop can continue on for infinity and design out of this. Perhaps by including a count that breaks the loop once an unrealistic loop count has been reached.




Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.
Tkinter - Learn the key features of the tkinter to allow you to create user interfaces for your python programs.


Related Pages

Pickle - The pickle library allows a program to save data in a binary file.
Python - List Comprehension - Create powerful list comprehension expressions.
Python - Generators - Learn about generator statements.
Python - Data types - The learn about the key datatypes in the python language.
Tkinter - Combo Box - This is a short example showing how to use combo boxes in a GUI.
Tkinter - Radio Buttons - A short example of how to create a GUI with radio buttons.
Tkinter - Canvas - Canvas allows for a range of shapes to be plotted, such as circles, squares, lines