If you couldn't create loops, you'd often have to write hundreds or thousands of lines of code instead. The basic idea of all types of loops is the same to reduce the number of code statements needed to repeatedly perform a task. Loops come in a number of different flavors, and you should use the appropriate type in any given situation. Some loops execute a specific number of times, while others execute as long as a certain condition is True. Using the correct loop for a job can mean more solid, efficient, and readable code.
Tip
One key to writing efficient code is to eliminate redundancy. If you find yourself typing the same (or similar) lines of code repeatedly, chances are good that you should create a loop.
Goals of Using Looping StructuresLooping structures are great for
|