# control flow

### `if` / `else`

```csharp
if (player.health < 5) {
    player.sendMessage("&clow health!")
} else if (player.health < 10) {
    player.sendMessage("&egetting low...")
} else {
    player.sendMessage("&ayoure ok :D")
}
```

braces are always required, even for a single statement:

```csharp
// this will not compile: no braces
if (x > 0)
    doSomething()
```

```csharp
// always do this
if (x > 0) {
    doSomething()
}
```

### `when`

`when` compares one value against several possibilities, top to bottom, and runs the first match (like a switch statement):

```csharp
when (color) {
    "red" -> {
        player.sendMessage("&cred team!")
    }
    "blue" -> {
        player.sendMessage("&9blue team!")
    }
    else -> { // this else statement is optional
        player.sendMessage("&7unknown team")
    }
}
```

### `return`

```csharp
function clamp(value, min, max) {
    if (value < min) {
        return min
    }
    if (value > max) {
        return max
    }
    return value
}
```

a function with no `return` returns `null`. `return` with no value is also valid:

```csharp
function startGame() {
    if (gameRunning) {
        return // already running, do nothing
    }
    gameRunning = true
    ...
}
```

this is especially important inside `Katton.after`/`Katton.every` callbacks, see [Katton.after and Katton.every](../scheduling/after-and-every.md) for why you almost always want a `return`
