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.
Conditional loop A conditional loop (also known as an indeterminate loop) is a loop that determines whether to terminate based on a logical condition. These loops are flexible, but their exact behavior can be difficult to reason about. A conditional loop is usually composed of two parts: a condition and a body. The condition is a logical statement depending on the state of the program and the body is a block of code that runs as long as the condition holds. A common misconception is that the execution of the body terminates as soon as the condition does not hold anymore; but this is usually not the case. In most programming languages, the condition is checked once for every execution of the body. When the condition is checked is not standardized and some programming languages contain multiple conditional looping structures with different rules about when the condition is assessed.
Pre-test loop A pre-test loop is a conditional loop where the condition is checked before the body is executed. More precisely, the condition is checked and if it holds the body is executed. Afterwards, the condition is checked again, and if it holds the body is executed again. This process repeats until the condition does not hold. Many programming languages call this loop a while loop and refer to it with the keyword while. They are commonly formatted in manner similar to
while condition do body repeat
Instead of the keywords do and repeat, other methods are sometimes used to indicate where the body begins and ends, such as curly braces or whitespace. For example, the following code fragment first checks whether x is less than five, which it is, so the body is entered. There, x is displayed and then incremented by one. After executing the statements in the body, the condition is checked again, and the loop is executed again. This process repeats until x has the value five.
x ← 0 while x < 5 do display(x) x ← x + 1 repeat
Post-test loop
A post-test loop is a conditional loop where the condition is checked after the body is executed. More precisely, the body is executed and afterwards the condition is checked. If it holds the body is run again and then the condition is checked. This is repeated until the condition does not hold. This is sometimes called a do-while loop due to the syntax used in various programming languages, although this can be confusing since Fortran and PL/I use the syntax "DO WHILE" for pre-test loops. Post-test loops are commonly formatted in manner similar to
do body repeat while condition
Instead of the keywords do and repeat others methods are sometime use to indicate where the body begins and ends, such as curly braces. Some languages may use a different naming convention for this type of loop. For example, the Pascal and Lua languages have a "repeat until" loop, which continues to run until the control expression is true and then terminates.
Three-part for loop
A three-part for loop, popularized by C, has two additional parts: initialization (loop variant), and increment, both of which are blocks of code. The initialization is intended as code that prepares the loop and is run once in the beginning and increment is used to update the state of the program after each iteration of the loop. Otherwise, the three-part for loop is a pre-test loop. They are commonly formatted in manner similar to
for initialization, condition, increment do body repeat
This syntax came from B and was originally invented by Stephen C. Johnson. The following C code is an example of a three part loop that prints the numbers from 0 to 4.
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
Enumeration
An enumeration (also known as an determinate loop) is a loop intended to iterate over all the items of a collection. It is not as flexible as a conditional loop; but it is more predictable. For example, it is easier to guarantee that enumerations terminate and they avoid potential off-by-one errors. Enumerations can be implemented using an iterator, whether implicitly or explicitly. Depending on the programming language, various keywords are used to invoke enumerations. For example, descendants of ALGOL use for, while descendants of Fortran use do and COBOL uses PERFORM VARYING. Enumerations are sometimes called "for loops," for example in Zig and Rust. This can be confusing since many of the most popular programming languages, such as C, C++, and Java, use that term for the three-part for loop, which is not an enumeration. Other programming languages, such as Perl and C#, avoid this confusion by using the term "foreach loop." The order in which the items in the collection are iterated through depends on the programming language. Fortran 95 has a loop, invoked using the keyword FORALL, that is independent of this order. It has the effect of executing each iteration of the loop at the same time. This feature was made obsolescent in Fortran 2018.
Loops in functional programming
… excerpt ends here. Continue reading the full article.


