# Statements and control flow

> Semicolons, scopes without shadowing, if, the three kinds of for, brk, nxt, ret, defer and the check that every path returns.

## Semicolons

A semicolon terminates a statement. Newlines are whitespace and never terminate anything, so a statement may span lines freely.

- Simple statements end with `;`: variable declarations, assignments, calls, `ret`, `brk`, `nxt`, `defer`. This includes the last statement of a block.
- Record and union fields end with `;`.
- Declarations without a body end with `;`: `use`, constants, and `ext` imports.
- Anything that ends with a `}` block takes no semicolon: functions, `rec`, `enum`, `union`, `if`, `for`, `vec`, `par`, `match`, `when`, `asm`, `test`, `bench`.
- Enum variants, parameters and arguments are separated by commas; a trailing comma is allowed.

An expression statement must be a call. `a + 1;` and `true;` are errors because they do nothing.

## if

The condition is a `bool` and needs no parentheses, because `{` never continues an expression.

```lane
i32 pick(i32 n) { i32 r = 9; if n == 1 { r = 1; } elif n == 2 { r = 2; } ret r; }
```

Verified: part of a program that builds and runs. For the whole program, exit code 129. No stdout.

## for

There are three loops, all written `for`.

- `for cond { }` runs while the `bool` condition holds.
- `for { }` runs until a `brk` or a `ret`.
- `for i in a..b { }` runs `i` from `a` up to but not including `b`; `a..=b` includes `b`, which may be the largest value of the type.

```lane
u32 n = 0; u32 odd = 0;
for n < 10 { n += 1; if n % 2 == 0 { nxt; } odd += n; }
u32 k = 0; u32 hits = 0;
for { k += 1; if k > 20 { brk; } if k % 3 != 0 { nxt; } hits += 1; }
```

Verified: part of a program that builds and runs. For the whole program, exit code 2506. No stdout.

`brk` leaves the innermost loop. `nxt` starts its next iteration: the next value of a range, or the condition of `for cond { }` again. Outside a loop both are errors.

```lane
nul main() { brk; }
```

Verified: the compiler refuses this program (exit code 1). Compiler output:

```text
main.ln:1:14: error: `brk` outside a loop
```

The two bounds of a range are integers of the same type, each evaluated once, `a` then `b`, before the first iteration. A literal bound takes the type of the other bound; two literal bounds give no type and are an error, so write `u32(0)..10`.

```lane
i32 main() { u32 s = 0; for i in u32(1)..=5 { s = s * 10 + i; } ret i32(s); }
```

Verified: builds and runs. Exit code 12345. No stdout.

```lane
nul main() { for i in 0..10 { } }
```

Verified: the compiler refuses this program (exit code 1). Compiler output:

```text
main.ln:1:23: error: the type of this integer is not known here; convert it, as in `u32(1)`
```

A range that starts past its end runs no iteration. The loop variable has the type of the bounds, is visible in the body only and is read-only.

```lane
nul main() { for i in u32(0)..3 { i = 1; } }
```

Verified: the compiler refuses this program (exit code 1). Compiler output:

```text
main.ln:1:35: error: loop variable `i` is read-only
```

The bounds are evaluated once, which a program can show with output:

```lane
u32 bound([]u8 name, u32 v) { io.print(name); ret v; }
nul main() {
    u32 n = 3;
    for i in bound("a", 0)..bound("b", n) { n += 10; io.print("."); }
    for i in bound("c", 1)..=bound("d", 2) { io.print(":"); }
    for i in bound("e", 2)..bound("f", 2) { io.print("!"); }
    io.print("\n");
}
```

Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout:

```text
ab...cd::ef
```

Iterating over a slice, `for c in text`, appears in a syntax example, but the specification does not define it (it says only "over a range or sequence"), and the compiler refuses a `for ... in` over anything but a range.

## Scopes

Every `{ }` is a scope: a function body, a branch, a loop body, or a block on its own. A variable is visible from its declaration to the end of its block. A name that is visible, parameters and loop variables included, cannot be declared again: there is no shadowing. After its block ends the name is free.

```lane
nul main() { u32 a = 1; if true { a := true; } }
```

Verified: the compiler refuses this program (exit code 1). Compiler output:

```text
main.ln:1:35: error: `a` is already declared
```

```lane
nul main() { for i in u32(0)..3 { } u32 b = i; }
```

Verified: the compiler refuses this program (exit code 1). Compiler output:

```text
main.ln:1:45: error: unknown name `i`
```

## ret, and every path must return

`ret value;` leaves the function; in a function that returns `nul` write `ret;`. A function that returns a value must end every path with `ret`. The check looks at the shape of the code and never at the value of a condition. A statement can be passed unless it is a `ret`, `brk` or `nxt`; an `if` with an `else` where neither branch can be passed; a `for { }` that contains no `brk` of its own; or a block containing such a statement. So `for true { }` can be passed and `for { }` cannot.

```lane
i32 f(bool c) { for { if c { ret 1; } } } nul main() { }
```

Verified: builds and runs. Exit code 0. No stdout.

```lane
nul main() { } 
i32 g() { for { brk; } }
```

Verified: the compiler refuses this program (exit code 1). Compiler output:

```text
main.ln:2:1: error: `g` returns `i32`, but not every path ends with `ret`
```

```lane
i32 main() { if true { ret; } ret 0; }
```

Verified: the compiler refuses this program (exit code 1). Compiler output:

```text
main.ln:1:24: error: `main` returns `i32`; `ret` needs a value
```

## match

`match` is covered with enums in [Records, enums and match](/docs/records-enums.md). It takes part in the return and `out` analyses as if it were an `if` and `elif` chain.

## defer

`defer` runs a statement at scope exit, for the rare owned resource. Memory itself comes from arenas, not from `defer`; see [Memory](/docs/memory.md).

```lane
bool load([]u8 path) {
    f := io.open(path);
    if f == nil { ret false; }
    defer io.close(f);
    ret true;
}
```

The functions `io.open` and `io.close` here stand for any resource functions; they are illustrative.

## Compound assignment

`a op= b` needs a variable of an integer type and a `b` of that type. It evaluates `b`, then reads `a` and stores `a op b`: in `a[i] += f()`, a change that `f` makes to `a[i]` is seen.
