Loop (statement)
This article is currently slated for merging. There is consensus to merge Foreach loop, For loop, While loop and Do while loop into this article. You can carry out the merge by following the resolution at the discussion and the merging instructions. Process started December 2025. |
In computer programming, a loop is a control flow statement that allows code to be executed repeatedly; usually with minor alterations between repetitions. Loops can be used to perform a repeated action on all objects in a collection, or to implement a long lived program.
Overview
[edit]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.[citation needed]
Different types of loops
[edit]Conditional loops
[edit]A conditional loop typically consists of a condition and a body. The body is code that is run repeatedly until the condition becomes false.
While loops
[edit]In a while loop the condition is initially checked, and if it is true the body is run. This is repeated until the condition is false. While loops are commonly formatted in manner similar to
while condition do
body
repeat
Instead of the keywords do and repeat others methods are sometime use to indicate where the body begins and ends, such as curly braces[citation needed] or whitespace.[citation needed]
See also
[edit]- Primitive recursive function
- General recursive function
- LOOP (programming language) – a programming language with the property that the functions it can compute are exactly the primitive recursive functions