Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Iteration
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this 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". === Implementations === [[Control flow#Loops |Loops]] constitute the most common language constructs for performing iterations. The following [[pseudocode]] "iterates" three times the line of code between begin & end through a ''for loop'', and uses the values of ''i'' as increments. <syntaxhighlight lang="pascal"> a := 0 for i := 1 to 3 do { loop three times } begin a := a + i; { add the current value of i to a } end; print(a); { the number 6 is printed (0 + 1; 1 + 2; 3 + 3) } </syntaxhighlight> 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. [[Iterators]] constitute alternative language constructs to loops, which ensure consistent iterations over specific data structures. They can eventually save 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. [[Iteratees]] are purely functional language constructs, which accept or reject data during the iterations. === Relation with recursion === Recursions and iterations have different algorithmic definitions, even though they can generate identical effects/results. 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 a 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 of 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 the 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)|Scheme]] programming language that will output the same result as the pseudocode under the previous heading. <syntaxhighlight lang="scheme"> (let iterate ((i 1) (a 0)) (if (<= i 3) (iterate (+ i 1) (+ a i)) (display a))) </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Iteration
(section)
Add topic