Top Qs
Timeline
Chat
Perspective
Loop (statement)
Control flow statement for executing code repeatedly From Wikipedia, the free encyclopedia
Remove ads
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.
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. |
Remove ads
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.[citation needed]
A conditional loop typically consists of a condition and a body. The body is code that is run repeatedly until the condition becomes false.
Remove ads
While loops
Summarize
Perspective

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]
A while consists of a block of code and a conditional expression.[1] The conditional is evaluated, and if true,[1] the block of code is executed. This repeats until the conditional becomes false. Because the while loop checks the conditional before the block is executed, the control structure is also known as a pre-test loop. In contrast, do-while loop tests the conditional after the block.
For example, in the languages C, Java, C#,[2] Objective-C, and C++, (which use the same syntax in this case), the code fragment
int x = 0;
while (x < 5) {
printf ("x = %d\n", x);
x++;
}
first checks whether x is less than 5, which it is, so the loop body is entered, where printf() is called and x is incremented by 1. After completing the statements in the loop body, the condition, (x < 5), is checked again, and the loop is executed again. This process repeats until x has the value 5.
The condition can always valuate as true to create an infinite loop. In this case, there may be a early-exit control structure (such as a break statement) that controls termination of the loop. For example:
while (true) {
// do complicated stuff
if (someCondition)
break;
// more stuff
}
Do while loops
Remove ads
For loops
See also
- 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
References
Wikiwand - on
Seamless Wikipedia browsing. On steroids.
Remove ads