Loop
- In Turkish
- Döngü
In short
A loop is a control structure that repeats a block of code, either a set number of times, once for each item in a collection, or while a condition stays true.
What is a loop in programming?
A loop tells the computer to run the same instructions again and again, so you don't have to write them out by hand. Each pass through the loop is called an iteration. Loops are how programs process every item in a list, retry a failed request, or keep a game running frame after frame.
Most languages offer a few kinds of loops. A for loop usually runs a set number of times using a counter, a while loop keeps going as long as a condition is true, and a for-each style loop, such as for...of in JavaScript or for item in items in Python, visits each element of a collection. Inside a loop, break stops the loop early and continue skips straight to the next iteration.
Walking laps around a track is a simple analogy for a loop: you repeat the same circuit, counting as you go, until you reach your target number of laps. The most common loop bug is an infinite loop, where the stopping condition never becomes false, so the program hangs. Another is the off-by-one error, where a loop runs one time too many or too few, often from writing <= instead of <.
Loops are closely related to recursion, since both repeat work, and anything written with one can be rewritten with the other. Loops are also often compared with array methods like map, filter, and forEach, which hide the loop inside a function call and describe what should happen to each item rather than how to step through them.
Key takeaways
- A loop repeats a block of code; each repetition is called an iteration.
forloops count,whileloops check a condition, and for-each loops visit every item in a collection.breakexits a loop early, andcontinueskips to the next iteration.- Infinite loops and off-by-one errors are the most common loop bugs.
Example
const scores = [72, 88, 95];
// for: runs a set number of times using a counter
for (let i = 0; i < scores.length; i++) {
console.log(`Score ${i + 1}: ${scores[i]}`);
}
// for...of: visits each item in the array directly
for (const score of scores) {
if (score < 80) continue; // skip to the next item
console.log("High score:", score);
}
// while: repeats as long as the condition is true
let lives = 3;
while (lives > 0) lives -= 1;Readers ask
What is the difference between a for loop and a while loop?
A for loop is best when you know how many times to repeat, since it keeps the counter, the condition, and the update together in one line. A while loop is best when you don't know in advance, such as reading input until the user types quit.
What is an infinite loop?
An infinite loop is a loop whose stopping condition never becomes false, so it runs forever and can freeze the program. It is usually caused by forgetting to update the loop variable or by writing a condition that is always true.
What is the difference between for...in and for...of in JavaScript?
for...of loops over the values of an iterable, such as an array or a string. for...in loops over the property names (keys) of an object and is generally not recommended for arrays.
See also
- ArrayProgramming Fundamentals, p. 3An array is an ordered collection of values stored under one name, where each item is accessed by its numeric position, called an index, usually starting at 0.
- RecursionProgramming Fundamentals, p. 48Recursion is a technique in which a function solves a problem by calling itself on smaller versions of the same problem until it reaches a simple base case.
- AlgorithmProgramming Fundamentals, p. 2An algorithm is a finite, step-by-step set of instructions for solving a problem or completing a task, such as sorting a list or finding the shortest route.
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- Big O NotationProgramming Fundamentals, p. 6Big O notation describes how an algorithm's running time or memory use grows as its input gets larger, focusing on the growth rate rather than exact speed.
- IteratorProgramming Fundamentals, p. 31An iterator is an object that lets code step through the items of a collection one at a time, remembering its position, without exposing how the data is stored.
Spotted a mistake or something missing on this page?Suggest an edit