[Previous] [Next]
Chapter 10
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 types, and you should use the appropriate type in each situation. Some loops execute a specific number of times, while others execute as long as a certain condition is met. Using the correct loop for a job can mean more solid, efficient, and readable code.
NOTE
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 Structures
Looping structures are great for
- Reducing repetitive code
- Iterating over an array or a collection
- Increasing the speed and efficiency of your code