# for

> The C-style counting loop. Runs init once, then repeats the body while cond holds, running update after each pass. Any clause may be omitted; for (;;) is infinite. continue runs the update before re-testing.

<!-- id: function-for-loop · area: function · stability: stable · html: https://osysharp.com/reference/function/for-loop/ -->

## Summary        {#summary}
`for (init; cond; update) { … }` is the C-style counting loop. It runs `init` once, then repeats the body
as long as `cond` holds, running `update` after each pass. Any of the three header clauses may be omitted;
`for (;;)` is an infinite loop (exit it with `break`). `continue` runs the `update` before re-testing the
condition — the same as C#.

## Signature      {#signature}
```osy syntax
for (<init>; <cond>; <update>) {
  …
}
// init:   a declaration (loop-scoped) or an expression, or empty
// cond:   a Boolean expression, or empty (= always true)
// update: an expression run after each iteration, or empty
```

## Description    {#description}
- **`init`** runs once before the loop. A declaration there (`int i = 0`) is **scoped to the loop** — it is
  not visible after the `for`.
- **`cond`** is re-tested before each iteration; an empty condition is always true (`for (;;)`).
- **`update`** runs after each body pass **and on `continue`** — so `continue` advances the counter, it does
  not skip it. (This is why `for` is a first-class construct, not a `while` rewrite.)
- **`break`** exits the loop; **`continue`** jumps to the update then the next condition test.
- The `update` is typically `i++` or `i += n` (see [++ / -- (increment / decrement)](https://osysharp.com/reference/function/increment-decrement/) /
  [Compound assignment (+= -= *= /= %= ??=)](https://osysharp.com/reference/function/compound-assignment/)).
- Durable: a suspend inside a `for` body survives serialize/restore — the loop resumes at the right
  iteration and still runs its update.

## Examples       {#examples}
```osy title="for loops" test app=for-loop
int Sum() { int s = 0; for (int i = 0; i < 10; i++) { s = s + i; } return s; }   // 45

// continue still runs the update — sums the even indices (no infinite loop).
int Evens() {
  int s = 0;
  for (int i = 0; i < 10; i++) {
    if (i % 2 == 1) { continue; }
    s = s + i;
  }
  return s;                                   // 0+2+4+6+8 = 20
}

// Nested loops.
int Grid() {
  int s = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) { s = s + i * j; }
  }
  return s;                                   // 9
}

// Infinite for + break (counter advanced in the body).
int Countdown() {
  int i = 0;
  for (;;) { if (i >= 7) { break; } i++; }
  return i;                                   // 7
}
```

## See also       {#see-also}
- [while](https://osysharp.com/reference/function/while-loop/) — the condition-only loop
- [foreach](https://osysharp.com/reference/function/foreach/) — iterate a collection's elements
- [++ / -- (increment / decrement)](https://osysharp.com/reference/function/increment-decrement/) — the usual `update` clause
- [break / continue](https://osysharp.com/reference/function/break-continue/) — loop control
