# Loop

URL: https://softwaredictionary.org/terms/loop
Category: Programming Fundamentals
Last updated: 2026-09-30
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.
- `for` loops count, `while` loops check a condition, and for-each loops visit every item in a collection.
- `break` exits a loop early, and `continue` skips to the next iteration.
- Infinite loops and off-by-one errors are the most common loop bugs.

## Example: Three kinds of loops in JavaScript

```javascript
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;
```

## Frequently asked questions

**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.

---

Software Dictionary: https://softwaredictionary.org/ · https://softwaredictionary.org/llms.txt
