> ## Documentation Index
> Fetch the complete documentation index at: https://requestscript.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Statements and control flow

> The statements a script body can contain, including if/else and return.

A script body is a sequence of statements that run from top to bottom. These are the available statements:

| Statement | Example |
| - | - |
| [Variable declaration](/language/variables) | `var count = 0`, `const limit = 10` |
| [Resource binding](/resources/overview#bind-a-resource) | `const weather: com.example.Weather` |
| [Assignment](/language/variables#assignment-targets) | `count = 1`, `obj.prop = 2`, `list[0] = 3` |
| Expression statement | `audit.log(message: "started")` |
| `if` / `else if` / `else` | See below |
| `return` | `return total` |

An expression statement evaluates an expression and discards the result. It's mainly useful for calling a resource function for its side effect.

There are no loops and no user-defined functions.

## if statements

An `if` statement runs its body only when its condition is `true`. The condition must be in parentheses and the body in curly braces.

```kotlin theme={null}
request Check {
    const amount = 120
    if (amount > 100) {
        return "large"
    }
    return "small"
}
```

Add `else if` branches to test more conditions in order, and an `else` branch to run when no condition matched:

```kotlin theme={null}
contract grades.Letter(score: int32, late: boolean) {
    if (score >= 90 && !late) {
        return "A"
    } else if (score >= 80) {
        return "B"
    } else {
        return "C"
    }
}
```

* The condition must evaluate to a `boolean`. `if (1)` or `if (name)` is an error: write `if (name != null)` instead.
* Curly braces are always required, even for a single statement.
* Each body is its own [scope](/language/variables#scope).

## return

`return` ends the script immediately. Its value becomes the `returnValue` of the response, and any statements after it are skipped.

```kotlin theme={null}
request EarlyReturn {
    const user = { name: null }
    if (user.name == null) {
        return "anonymous"
    }
    return user.name
}
```

A script returns `null` if it uses a bare `return` or reaches the end of its body without a `return`.

> [!WARNING]
>
> The returned value must start on the same line as `return`. A value on the next line isn't part of the `return`, so the script returns `null`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.