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

Reference / Function

while

while (<condition>) { … }

Repeats while a bool condition holds. Reach for it when the number of iterations is not known up front — otherwise a foreach or a for loop says more.

stable2 examples compiled by CIfunctioncontrol-flow

Summary#

while repeats its body as long as the condition is true, checked before each pass — so a condition that is false at the start means the body never runs. The condition must be a bool.

Signature#

while (<bool>) { … }

Description#

while, foreach, or for — which loop?#

Use while when you do not know up front how many passes you need — consuming until something is exhausted, converging on a value. When you are walking a collection, foreach says it better; when you are counting, so does for.

int TimesToHalve(decimal amount, decimal limit) {
  var steps = 0;
  var current = amount;
  while (current > limit) {
    current = current / 2;
    steps += 1;
  }
  return steps;
}

Advance the condition, or it never ends#

The body must move the condition towards false. A while whose condition never changes is an infinite loop, and the platform will not save you from it — it will simply run until it is stopped. Make the thing the condition reads the thing the body changes, and keep them close enough to see together.

How do I stop part-way through?#

break leaves the loop; continue skips to the next check. See break / continue.

int CountUpTo(int limit) {
  var i = 0;
  while (true) {
    i += 1;
    if (i >= limit) { break; }     // the exit is explicit, and easy to find
  }
  return i;
}

See also#

Related

foreach

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

for

The C-style counting loop. Runs init once, then repeats the body while cond holds, running update after each pass. Any…

break / continue

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

if / else

Conditional branching, exactly as in C#. The condition must be a bool — there is no truthiness, so a null or a number…