Why Is Looping Important to All Programmers?
All of the major programming languages support looping. For loops and while loops are among the most common. Loops allow programmers to define iterative structures so that certain excerpts of code will execute repeatedly. Looping is important for a number of reasons. Programmers use loops for processing data structures and other input. Loops also allow programmers to reuse the code they write.
-
Code Reuse
-
Computer programming can be a complex, time-consuming task. For this reason, programmers often look to minimize the amount of code necessary to delivery the functionality within an application. Repeating common sections of code is one way to reduce the amount of programming necessary. This also means that if a code process needs changed, the programmer only has to change it once. For loops and while loops instruct the program or Web browser to carry out the same process multiple times.
Control Flow
-
Loops are among the main control structures in programming. Most programs do not simply iterate one line at a time, moving in a linear fashion through a script. The following code demonstrates a sample PHP for loop:
for($count=0; $count<10; $count++) {
echo "<p>".$count."</p>";
}This loop iterates 10 times, starting at zero and stopping at nine. The content is trivial to demonstrate, but loops in actual programs can contain many lines of complex processing. A loop is therefore one of the basic ways in which you can implement complex flows of execution, with multiple processes encapsulated in a few lines of code.
-
Data Structures
-
Many programs use loops to iterate through data structures such as arrays. Arrays store data in linear structures, with each item at a unique position. The positions are normally represented as integer number values. For example, the following sample JavaScript code demonstrates accessing an element in an array:
myArray[3];This indicates the fourth position in the array, as the first is at position zero. The following for loop accesses each element in the array once:
var index;
for(index=0; index<myArray.length; index++) {
document.write(myArray[index] + "<br/>");
}This code writes the value of each element in the array on a separate line. Loops are often involved in managing data structures and collections.
Input Variables
-
Programs and websites often read data from external input sources. When a programmer writes an application, they may not know much about the content being imported. While loops are commonly involved in these situations, as you can set a loop up to continue reading an input file or other resource until there is nothing left to read. The following Java excerpt demonstrates a while loop:
while(scanner.hasNext()) {
System.out.println(scanner.nextLine());
}This code will continue reading and outputting the content of an input file using a scanner object. When the file has no more data to read, the loop will stop iterating.
-
References
Resources
- Photo Credit Zedcor Wholly Owned/PhotoObjects.net/Getty Images