> For the complete documentation index, see [llms.txt](https://dhrubo.gitbook.io/programming-with-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dhrubo.gitbook.io/programming-with-js/basic-syntax/loops.md).

# Loops

In computer programming, a loop is a control flow statement that allows a section of code to be executed repeatedly based on a certain condition. Loops are used to iterate through a block of code multiple times, allowing the programmer to avoid writing the same code over and over again.

There are different types of loops in programming, but the most common ones are:

1. `for` loop: This loop is used when you know how many times you want to execute the code. The basic syntax of the `for` loop is:

```javascript
for (initialization; condition; increment/decrement) {
  // code to be executed
}
```

For example, the following code uses a `for` loop to print the numbers from 1 to 5:

```javascript
for (let i = 1; i <= 5; i++) {
  console.log(i);
}
```

2. `while` loop: This loop is used when you want to execute the code as long as a certain condition is true. The basic syntax of the `while` loop is:

```javascript
while (condition) {
  // code to be executed
}
```

For example, the following code uses a `while` loop to print the numbers from 1 to 5:

```javascript
let i = 1;
while (i <= 5) {
  console.log(i);
  i++;
}
```

3. `do-while` loop: This loop is similar to the `while` loop, but it executes the code at least once before checking the condition. The basic syntax of the `do-while` loop is:

```javascript
do {
  // code to be executed
} while (condition);
```

For example, the following code uses a `do-while` loop to print the numbers from 1 to 5:

```javascript
let i = 1;
do {
  console.log(i);
  i++;
} while (i <= 5);
```

These are the most commonly used loops in JavaScript, but there are other types of loops as well, such as the `for...in` loop and the `for...of` loop.
