Documentation

Control Flow

Control Flow

Arx supports if/elif/else, while, and two for loop styles.

Declaration Scopes

Each if, elif, or else arm and each loop body has a lexical declaration scope. A declaration is visible only in its enclosing scope and nested bodies; it cannot be read after that scope ends, even if that body always executes. Sibling bodies may reuse a name, and inner declarations may shadow outer ones. Assignment without a declaration updates the nearest visible binding instead. This differs from Python's function-wide local declarations.

Declaring the same name twice in one scope is an error. Function parameters and the top-level function body share a scope; a loop target and its body also share their loop scope. Leaving a body releases its local owners on fallthrough or control transfer, without releasing shadowed outer owners. See the ownership contract for resource-specific rules.

If / Elif / Else

Conditions must be Boolean and are evaluated in order, stopping at the first match. A chain may have any number of elif clauses and an optional final else. Align every clause with its opening if; use elif, not else if.

```
title: Conditional chain example
summary: Classifies an integer using ordered comparisons.
```
fn classify(value: i32) -> i32:
  ```
  title: classify
  summary: Classifies one integer as negative, zero, or positive.
  ```
  if value < 0:
    return 0 - 1
  elif value == 0:
    return 0
  else:
    return 1

While Loop

```
title: While loop example
summary: Repeats while condition is true.
```
fn count_to_ten() -> i32:
  ```
  title: count_to_ten
  summary: Increments value until it reaches ten.
  ```
  a: i32 = 0
  while a < 10:
    a = a + 1
  return a

For Loop (For-In Style)

For-in loops iterate over ranges and supported sequences, including list literals and list variables:

for i in <iterable-expression>:

```
title: Range-style for loop
summary: Iterates from start to end with the builtin range helper.
```
fn range_loop(n: i32) -> i32:
  ```
  title: range_loop
  summary: Sums values produced by a range loop.
  ```
  total: i32 = 0
  for i in range(0, n):
    total = total + i
  return total

range(stop) and range(start, stop[, step]) produce lazy, stop-exclusive integer progressions. Named start, stop, and step are supported. All signed and unsigned integer widths from 8 through 64 bits are accepted. The step has its own type, so a negative step can count down between unsigned bounds. Zero step raises AssertionError, even for an empty interval.

Iteration and indexing yield the element type of range[T]. A bare range annotation means range[i32]. Without an explicit type argument, the bounds select the smallest lossless common integer type. A literal uses the type of a concrete bound when it fits. Otherwise ordinary literals use i32 when they fit, widening to i64 or u64 for larger values. For example, range(len(values)) yields i64 indices.

Use range<T>(...) to choose the element type explicitly, including for bounds with no common integer type. The first and last yielded values must fit T, and construction raises OverflowError before publishing a range that would yield an unrepresentable value. range<u8>(256) yields 0 through 255. An empty progression has no element to check.

Storage is constant-size, and each iteration has an independent cursor. Rebinding the input does not replace an active cursor's snapshot. Integer indexing checks bounds. Negative indices and slicing are unsupported. len returns i64 and raises OverflowError when the count exceeds that type. Indexing and iteration remain available for such a range.

Request list storage explicitly with list(values, max_items=N); exceeding the bound raises ValueError without publishing a truncated prefix. There is no implicit allocation when passing a range to a list or numeric-sequence consumer.

You can also iterate over a list literal or a list variable:

```
title: List for-in loop
summary: Iterates over one list variable and one list literal.
```
fn list_loop() -> i32:
  ```
  title: list_loop
  summary: Sums values from two list-valued loop inputs.
  ```
  values: list[i32] = list(range(3))
  total: i32 = 0
  for value in values:
    total = total + value
  for value in [3, 4]:
    total = total + value
  return total

Class values can supply their own cursors through the iteration protocol.

The old colon range header syntax was removed. Use builtin range(...) or any other supported iterable expression instead.

For Loop (Count Style)

Count-style loops follow:

for i: type = init; condition; update:

```
title: Count-style for loop
summary: Uses initializer, condition, and update expressions.
```
fn count_loop() -> i32:
  ```
  title: count_loop
  summary: Sums numbers from 0 to 4.
  ```
  total: i32 = 0
  for i: i32 = 0; i < 5; i += 1:
    total += i
  return total

Compound updates write the induction variable themselves. Ordinary expression updates such as i + 1 remain supported; the loop stores their result into i. See the compound assignment contract.

Break and Continue

break exits the nearest enclosing loop. continue starts its next iteration; in a count loop it runs the update first. Both are operand-free statements and are rejected outside loops. They work in while, count, and supported for-in loops, including nested loops.

```
title: Skip values and stop early
```
fn main() -> i32:
  total: i32 = 0
  for value in range(1000000):
    if value == 0:
      continue
    if value == 4:
      break
    total += value
  assert total == 6
  return 0

Loop transfers release exited-scope owners and run finally before advancing or leaving the loop. A cleanup failure cancels the transfer and propagates as an exception; an explicit finalizer can replace it with another transfer, return, or exception. Active iteration borrows end when their loop is exited.

Return Statements

Indentation Rules

Search documentation

Type to search.