(for-loop)=
# `for` loop

:::{wpd} for loop
a structured control flow statement that repeatedly runs a section of code until a condition is satisfied.
:::

:::{mermaid} ../img/for.mmd
:::


A `for` can be rewritten to a `while` and vice-versa. The difference: `for` can declare throwaway variables in its `init` field, which can be used to iterate (i.e., navigate) over a data structure:

::::{grid} 2
:::{grid-item-card} `for` iterates over an array
```{literalinclude} ../code/for-loop.c
:language: c
```
:::
:::{grid-item-card} `while` imitates a `for` loop
```{literalinclude} ../code/for-loop-imitation-using-while.c
:language: c
```
:::
::::

For example, both code describe the same behavior, but in the right code, 