# Control Flow

This page is the "Control Flow" section of the Voxlang language reference. It is generated from LANGUAGE.md in the Voxlang compiler's own repository, so it says what the specification says and nothing more.

A URL is the heading, lowercased, with punctuation dropped and spaces turned into hyphens. Each "##" section of the spec is a page under /docs/, and every heading inside it is a fragment on that page, so "File I/O" is /docs/file-io/ and "Reading a whole file" inside it is /docs/file-io/#reading-a-whole-file.

As data: https://vox-lang.dev/docs/index.json lists every section and every heading with its URL, https://vox-lang.dev/docs/search.json carries one entry per heading with its first sentence and keywords, and https://vox-lang.dev/docs/anchors.json maps every slug to the page it lives on. Search is also a JSON endpoint: https://vox-lang.dev/docs/search?q=<words> returns ranked results as JSON, no page load.

Source: LANGUAGE.md at bf2cba0, 2026-08-22. https://github.com/Vox-lang/vox/blob/bf2cba037976586c30d22a48e7b7246d347f54a1/LANGUAGE.md

[Reference](https://vox-lang.dev/docs/) > Control Flow

## If Statement

```vox fragment
If <condition> then, <statement>.
```

**With else:**

```vox fragment
If <condition> then, <statement>. Otherwise, <statement>.
```

**With else-if:**

```vox fragment
If <condition> then, <statement>. But if <condition> then, <statement>. Otherwise, <statement>.
```

**Sentence consumption rule (important):**

- Each `then,` / `but if ... then,` / `otherwise,` branch consumes actions until the sentence ends.
- Separate multiple actions in a branch with commas.
- Use a period to end the full `if` sentence.
- A period before `but if`/`otherwise` is treated as part of the same if-chain when the chain continues.

```vox fragment
If ready then, print "a", print "b", print "c".
```

**Alternative keywords:**

- `When` can replace `If`
- `Else` can replace `Otherwise`

## While Loop

```vox fragment
While <condition>, <statements>.
```

**Single-line example:**

```vox fragment
While the counter is less than 10, print the counter, increment the counter.
```

**Multi-action loops** are comma-separated actions within one sentence:

```vox fragment
While x is less than 5, print x, increment x, print "looping".
```

**Loops inside functions** work naturally:

```
To sum of a number called n.
  a number called total is 0.
  a number called i is 1.
  While i is less than or equal to n, total is total add i, i is i add 1.
  Return a number, total.
```

## For Each Loop

**Range-based:**

```vox fragment
For each number from <start> to <end>, <statement>.
```

**Example:**

```
For each number from 1 to 10, print the number.
```

**Inside the loop:**

- `the number` refers to the current iteration value

**List-based:**

```vox fragment
For each <variable> in <list>, <statement>.
```

**Example:**

```
a list called nums is [1, 2, 3].
For each n in nums, print the n.
```

## Repeat

Run a body a fixed number of times.

```vox fragment
Repeat <count> times, <statements>.
```

**Single-line example:**

```vox fragment
Repeat 3 times, print "hello".
```

**Multi-action loops** are comma-separated actions within one sentence, exactly like `While`:

```vox fragment
Repeat 2 times, print "a", print "b".
```

This prints `a`, `b`, `a`, `b` — two actions per iteration, two iterations.

**Termination.** `Repeat` closes by the same rules as `While` and `For each`: a period ends the body (and closes the construct — rule 1), and a blank line force-closes it (rule 2). The statements after a closing period belong to the surrounding scope, not the loop:

```vox
Repeat 2 times, print "r".
Print "after".
```

→ `r` `r` `after`

Because a period closes the construct, periods stack: write one period per level you want to close, so a `Repeat` nested in another loop takes two periods to close both (see [Closing more than one level](https://vox-lang.dev/docs/basics/#closing-more-than-one-level)):

```vox
For each n from 1 to 2,
    Repeat 2 times, print "r"..
Print "after".
```

→ `r` `r` `r` `r` `after`

## Loop Control

```vox fragment
Break.
Continue.
```

## Program Termination

Immediately exit the program with an exit code:

```vox fragment
Exit <code>.
```

**Examples:**

```
Exit 0.                              (Success)
Exit 1.                              (General error)

If arguments's empty then,
    Print "Usage: ./program <file>".
    Exit 1.
```

**Notes:**

- Exit code defaults to 0 if not specified
- All resources are automatically cleaned up before exit
- Alternative keywords: `quit`, `terminate`

## Increment/Decrement

```
Increment the counter.
Decrement the value.
```
