> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xano.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Loops

### <Icon icon="https://mintcdn.com/xano-997cb9ee/aZQYcxhIvSDTNEim/images/icons/xs_temp.svg?fit=max&auto=format&n=aZQYcxhIvSDTNEim&q=85&s=6e05b86a660544b2d6040353bd8faac8" size={46} width="371" height="137" data-path="images/icons/xs_temp.svg" /> **For Each Loop**

```xs theme={null}
foreach ($list) {
  each as alias {
    // XanoScript statements go here
  }
}
```

| Parameter | Purpose                                                         | Example |
| --------- | --------------------------------------------------------------- | ------- |
| \$list    | The list to iterate through                                     | \$users |
| \$alias   | The variable to store the item currently being iterated through | user    |

Place the functions that run as a part of your loop inside \{} brackets after defining the alias.

<Accordion title="Example">
  <img src="https://mintcdn.com/xano-997cb9ee/-vy8_DWVOwkWo8Bt/images/869acd28-image.jpeg?fit=max&auto=format&n=-vy8_DWVOwkWo8Bt&q=85&s=210b95e32a2eb42856dab2b67c690548" alt="" width="650" height="166" data-path="images/869acd28-image.jpeg" />

  ```xs theme={null}
  foreach ($x1) {
    each as item {
      util.sleep {
        value = 1
      }
    }
  }
  ```
</Accordion>

### <Icon icon="https://mintcdn.com/xano-997cb9ee/aZQYcxhIvSDTNEim/images/icons/xs_temp.svg?fit=max&auto=format&n=aZQYcxhIvSDTNEim&q=85&s=6e05b86a660544b2d6040353bd8faac8" size={46} width="371" height="137" data-path="images/icons/xs_temp.svg" /> **For Loop**

```xs theme={null}
for (`$loops`) {
  each as index {
    // XanoScript statements go here
  }
}
```

| Parameter | Purpose                              | Example |
| --------- | ------------------------------------ | ------- |
| \$loops   | The number of iterations in the loop | 10      |
| \$index   | The name of the index variable       | index   |

Place the functions that run as a part of your loop inside \{} brackets after defining the index variable.

<Accordion title="Example">
  <img src="https://mintcdn.com/xano-997cb9ee/nsvdyKK4Dg7VUAZs/images/8fb0bc0f-image.jpeg?fit=max&auto=format&n=nsvdyKK4Dg7VUAZs&q=85&s=6cec9eeeda4e3b0aa79ca5f08ac566b3" alt="" width="564" height="164" data-path="images/8fb0bc0f-image.jpeg" />

  ```xs theme={null}
  for (`10`) {
    each as index {
      util.sleep {
        value = 1
      }
    }
  }
  ```
</Accordion>

### <Icon icon="https://mintcdn.com/xano-997cb9ee/aZQYcxhIvSDTNEim/images/icons/xs_temp.svg?fit=max&auto=format&n=aZQYcxhIvSDTNEim&q=85&s=6e05b86a660544b2d6040353bd8faac8" size={46} width="371" height="137" data-path="images/icons/xs_temp.svg" /> **While Loop**

```xs theme={null}
while (`conditions`) {
  each {
    // XanoScript statements go here
  }
}
```

| Parameter  | Purpose                                                                     | Example                          |
| ---------- | --------------------------------------------------------------------------- | -------------------------------- |
| conditions | The condition(s) that the loop will use to determine if it continues to run | `false == false && true == true` |

Place the functions that run as a part of your loop inside \{} brackets after defining your conditions.

<Accordion title="Example">
  <img src="https://mintcdn.com/xano-997cb9ee/NAqNmVIgcJlXegps/images/00a8c1df-image.jpeg?fit=max&auto=format&n=NAqNmVIgcJlXegps&q=85&s=d63206037d61647b2bdcfeca9f48038e" alt="" width="852" height="174" data-path="images/00a8c1df-image.jpeg" />

  ```xs theme={null}
  while (`true == true && true == true`) {
    each {
      util.sleep {
        value = 1
      }
    }
  }
  ```
</Accordion>

### <Icon icon="https://mintcdn.com/xano-997cb9ee/aZQYcxhIvSDTNEim/images/icons/xs_temp.svg?fit=max&auto=format&n=aZQYcxhIvSDTNEim&q=85&s=6e05b86a660544b2d6040353bd8faac8" size={46} width="371" height="137" data-path="images/icons/xs_temp.svg" /> **Break Out Of Loop**

```xs theme={null}
break
```

<Accordion title="Example">
  <img src="https://mintcdn.com/xano-997cb9ee/NAqNmVIgcJlXegps/images/00a8c1df-image.jpeg?fit=max&auto=format&n=NAqNmVIgcJlXegps&q=85&s=d63206037d61647b2bdcfeca9f48038e" alt="" width="852" height="174" data-path="images/00a8c1df-image.jpeg" />

  ```xs theme={null}
  for (`10`) {
    each as index {
      conditional {
        if (`$index == 5`) {
          break
        }
      }
    }
  }
  ```
</Accordion>

### <Icon icon="https://mintcdn.com/xano-997cb9ee/aZQYcxhIvSDTNEim/images/icons/xs_temp.svg?fit=max&auto=format&n=aZQYcxhIvSDTNEim&q=85&s=6e05b86a660544b2d6040353bd8faac8" size={46} width="371" height="137" data-path="images/icons/xs_temp.svg" /> Continue to Next Iteration

```xs theme={null}
continue
```

<Accordion title="Example">
  <img src="https://mintcdn.com/xano-997cb9ee/rOuOq7qlTNyaIMAW/images/585529a9-image.jpeg?fit=max&auto=format&n=rOuOq7qlTNyaIMAW&q=85&s=aa07c597e9212aa2e4c54dd81b7f8c4c" alt="" width="688" height="320" data-path="images/585529a9-image.jpeg" />

  ```xs theme={null}
  for (`10`) {
    each as index {
      conditional {
        if (`$index == 5`) {
          continue
        }
      }
    }
  }
  ```

  ### Iterating Over an Array of Objects

  You can access properties of each item in a `foreach` loop:

  ```xs theme={null}
  var $users {
    value = [
      {id: 1, name: "Alice"},
      {id: 2, name: "Bob"}
    ]
  }

  foreach ($users) {
    each as user {
      debug.log {
        value = user.name
      }
    }
  }
  ```

  ### Nested Loops

  You can nest loops for multi-dimensional data:

  ```xs theme={null}
  var $matrix {
    value = [
      [1, 2],
      [3, 4]
    ]
  }

  foreach ($matrix) {
    each as row {
      foreach (row) {
        each as value {
          debug.log {
            value = value
          }
        }
      }
    }
  }
  ```

  ### When to Use For vs Foreach

  * Use `for` when you need a fixed number of iterations or an index variable.
  * Use `foreach` to iterate over each item in a list or array.

  ### Notes on Break and Continue

  The `break` and `continue` statements work in all loop types (`for`, `foreach`, and `while`). Use `break` to exit the loop early, and `continue` to skip to the next iteration.
</Accordion>
