Iteration
Iteration is a process in computer programming that repeats a function a set number of times, with the result of each iteration often feeding into the next. Iterative functions run the same code block repeatedly and automatically, processing multiple chunks of data in sequence without redundant code.
The most common forms of iteration used in computer programs are while loops and for loops. Loops run repeatedly as long as a specified condition is true. For example, a developer first creates a variable that tracks how many times the function has run, then sets a total number of loops. After each iteration finishes, it increments the tracking variable and runs again until the tracking variable reaches the total number previously set.
While the specific syntax of while and for loops varies in each programming language, they often follow a similar pattern. The syntax for these loops in Java looks like this:
While loop: while (i < 30) { ... i++; }
For loop: for (i=0; i < 30; i++) { ... }
Both samples first create a tracking variable, i, and a total number of times for the iteration to run. The while loop includes the incrementation command, i++, at the end of the function, then checks the condition again when it starts over. The for loop creates the tracking variable, the total number of iterations, and how to increment the tracking variable at the start.
Software developers use iterative loops for many common purposes. For example, a developer may use an iterative function to perform calculations on each value in an array, running once for each array address until it reaches the end. A web developer may use a PHP script with an iterative loop to pull lines from a database record, add HTML formatting to display that content in a table, then close the table after getting the final line in the database record.