# Iteration

5:21

Iteration is the repetition of a process in order to generate an outcome. The sequence will approach some end point or end value. Each repetition of the process is a single iteration, and the outcome of each iteration is then the starting point of the next iteration.

In mathematics and computer science, iteration (along with the related technique of recursion) is a standard element of algorithms.

## In mathematics

In mathematics, iteration may refer to the process of iterating a function, i.e. applying a function repeatedly, using the output from one iteration as the input to the next. Iteration of apparently simple functions can produce complex behaviors and difficult problems – for examples, see the Collatz conjecture and juggler sequences.

Another use of iteration in mathematics is in iterative methods which are used to produce approximate numerical solutions to certain mathematical problems. Newton's method is an example of an iterative method. Manual calculation of a number's square root is a common use and a well-known example.

## In computing

In computing, iteration is the technique marking out of a block of statements within a computer program for a defined number of repetitions. That block of statements is said to be iterated; a computer scientist might also refer to that block of statements as an "iteration".

The pseudocode below is an example of iteration; the line of code between the brackets of the for loop will "iterate" three times:

```a = 0
for i from 1 to 3        // loop three times
{
a = a + i            // add the current value of i to a
}
print a                  // the number 6 is printed (0 + 1; 1 + 2; 3 + 3)
```

It is permissible, and often necessary, to use values from other parts of the program outside the bracketed block of statements, to perform the desired function. In the example above, the line of code is using the value of i as it increments.

## Relationship with recursion

In algorithmic situations, recursion and iteration can be employed to the same effect. The primary difference is that recursion can be employed as a solution without prior knowledge as to how many times the action will have to repeat, while a successful iteration requires that foreknowledge.

Some types of programming languages, known as functional programming languages, are designed such that they do not set up block of statements for explicit repetition as with the for loop. Instead, those programming languages exclusively use recursion. Rather than call out a block of code to be repeated a pre-defined number of times, the executing code block instead "divides" the work to be done into a number separate pieces, after which the code block executes itself on each individual piece. Each piece of work will be divided repeatedly until the "amount" of work is as small as it can possibly be, at which point algorithm will do that work very quickly. The algorithm then "reverses" and reassembles the pieces into a complete whole.

The classic example of recursion is in list-sorting algorithms such as merge sort. The merge sort recursive algorithm will first repeatedly divide the list into consecutive pairs; each pair is then ordered, then each consecutive pair of pairs, and so forth until the elements of the list are in the desired order.

The code below is an example of a recursive algorithm in the Scheme programming language that will output the same result as the pseudocode under the previous heading.

```(let iterate ((i 1) (a 0))
(if (<= i 3)
(iterate (+ i 1) (+ a i))
(display a)))
```

## Other terminology

In object-oriented programming, an iterator is an object that ensures iteration is executed in the same way for a range of different data structures, saving time and effort in later coding attempts. In particular, an iterator allows one to repeat the same kind of operation at each node of such a data structure, often in some pre-defined order.

An iteratee is an abstraction which accepts or rejects data during an iteration.

## Education

In some schools of pedagogy, iterations are used to describe the process of teaching or guiding students to repeat experiments, assessments, or projects, until more accurate results are found, or the student has mastered the technical skill. This idea is found in the old adage, "Practice makes perfect." In particular, "iterative" is defined as the "process of learning and development that involves cyclical inquiry, enabling multiple opportunities for people to revisit ideas and critically reflect on their implication."[1]

Unlike computing and math, educational iterations are not predetermined; instead, the task is repeated until success according to some external criteria (often a test) is achieved.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://codedocs.org/what-is/iteration", "fetch_time": 1712958756000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-18/segments/1712296816070.70/warc/CC-MAIN-20240412194614-20240412224614-00320.warc.gz", "warc_record_offset": 158942665, "warc_record_length": 8575, "token_count": 953, "char_count": 4752, "score": 4.1875, "int_score": 4, "crawl": "CC-MAIN-2024-18", "snapshot_type": "longest", "language": "en", "language_score": 0.9063218235969543, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00031-of-00064.parquet"}