Loop (statement)

Uses your system voice; voice and rate in settings.

Snapshot

≈ 5 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

In computer programming, a loop is a control flow construct that allows code to be executed repeatedly, usually with minor alterations between repetitions. Loops can be used to perform a repeated action on all items in a collection, or to implement a long lived program.

In computer programming, a loop is a control flow construct that allows code to be executed repeatedly, usually with minor alterations between repetitions. Loops can be used to perform a repeated action on all items in a collection, or to implement a long lived program.

Overview

Loops are a feature of high-level programming languages. In low-level programming languages the same functionality is achieved using jumps. When a program is compiled to machine code, looping may be achieved using jumps; but some loops can be optimized to run without jumping. Usually, loops are expected to run for a finite number of iteration. Without proper care, loops may accidentally be created that have no possibility of terminating. Such loops are called infinite loops. The problem of determining whether a program contains an infinite loop is known as the halting problem. for (int i = 0; i

Equivalent constructs

Assuming there is a properly declared function or method called do_work() , the following are equivalent in programming languages which support post-test loops. Furthermore, given that a continue statement is not used, the above is technically equivalent to the following, though these examples are not typical or modern style used in everyday computers. while true do do_work() if condition is not true then break end if repeat or alternatively, LOOPSTART: do_work() if condition then goto LOOPSTART end if

Loop counter

A loop counter is a control variable that controls the iterations of a loop. Loop counters change with each iteration of a loop, providing a unique value for each iteration. The loop counter is used to decide when the loop should terminate. It is so named because most uses of this construct result in the variable taking on a range of integer values. A common identifier naming convention is for the loop counter to use the variable names i, j, and k (and so on if needed), where i would be the most outer loop, j the next inner loop, etc. This style is generally agreed to have originated from the early programming of Fortran , where these variable names beginning with these letters were implicitly declared as having an integer type, and so were obvious choices for loop counters that were only temporarily required. The practice dates back further to mathematical notation where indices for sums and multiplications are often i, j, and k. Using terse names for loop counters, like i and j, is discouraged by some since the purpose of the variables is not as clear as if they were given a longer more descriptive name. Different languages specify different rules for what value the loop counter will hold on termination of its loop, and indeed some hold that it becomes undefined. This permits a compiler to generate code that leaves any value in the loop counter, or perhaps even leaves it unchanged because the loop value was held in a register and never stored in memory. Actual behavior may even vary according to the compiler's optimization settings. Modifying the loop counter within the body of the loop can lead to unexpected consequences. To prevent such problems, some languages make the loop counter immutable. However, only overt changes are likely to be detected by the compiler. Situations where the address of the loop counter is passed as an argument to a subroutine, make it very difficult to check because the routine's behavior is in general unknowable to the compiler unless the language supports procedure signatures and argument intents.

Early exit and continuation

Some languages may also provide supporting statements for altering how a loop's iteration proceeds. Common among these are the break statement, which terminates the current loop the program is in, and the continue statement, which skips to the next iteration of the current loop. These statements may have other names; For example in Fortran 90, they are called exit and cycle. A loop can also be terminated by returning from the function within which it is being executed. In the case of nested loops, the break and continue statements apply to the inner most loop. Some languages allow loops to be labelled. These statements can then be applied to any of the loops in which the program is nested. outer_loop: (This is a label for the outermost loop) for 1 ≤ i ≤ 2 do for 1 ≤ j ≤ 2 do display(i, j) if i = 2 continue outer_loop end if repeat repeat (This nested loop displays the pairs (1, 1), (1, 2), and (2, 1))

Infinite loop

An infinite loop is a loop which never terminates. This can be intentional, or the result of a logic error. Systematically detecting infinite loops is known as the halting problem. Infinite loops are useful in applications which need to perform a repeated calculation until a program terminates, such as web servers.

References

Text derived from https://en.wikipedia.org/w/index.php?oldid=1370379282 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

24 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.