Osy#the first language built for agents
Agents firstAgentic appsWorkflowsDurable Execution — built inSecurityTestingThe editorThe UI modelOne program

Reference / Function

for

for (init; cond; update) { … }

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.

stable1 example compiled by CIfunctioncontrol-flowauthoring

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#

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#

  • 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) / 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#

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#

Related

while

Repeats while a bool condition holds. Reach for it when the number of iterations is not known up front — otherwise a…

foreach

Walks a collection — a query result, a list, or a parent's children. The normal way to iterate; reach for a for loop…

++ / -- (increment / decrement)

Increment or decrement a numeric variable or property by one. In statement position (i++;) and as a for increment the…

break / continue

break leaves the enclosing loop; continue skips the rest of this pass. Inside a switch, break leaves the SWITCH, not…