# Exit

`exit` breaks out of scopes until a loop or function call is reached.

## Concepts Used

* [Scopes](scopes.md)
* [Call Stack](call_stack.md)
* [Callable Parameters](callable_parameters.md)

## Overview

Usually the commands of a scope are executed until the end is reached. The exit statement can stop execution of the scope it is in.

```oto
loop (
  input is 'q then exit
)
```

## Description

**Note:** This is one of the more complex features of the language.

## Examples

**Stopping when the user pressed a key:**

```oto
loop (
  key is ('ctrl 'q) exit
)
```

**Returning a value from a function:**

```oto
number-name(number): (
  number is 1 then ('one exit)
  number is 2 then ('two exit)
  number is 3 then ('three exit)
)

number-name 1 should-be 'one
number-name 2 should-be 'two
```

**Exiting a custom iterator:**

```oto
range(from to body!): (
	from = current
	while (current < to) (
		current + 1 = current
		current body!
	)
)

range 1 10 (
  is 3 then exit
)
```

**Propagating an exit state:**

```oto
exit-on-3: (
  # Return a truthy value when the loop should exit.
  is 3
)

1 to 10 each (
  # Use the return value to determine if the loop should be stopped.
  exit-on-3 then exit
)
```

## Common Bugs

### Exit not breaking out of a scope

```oto
((exit) 1)
```

This will put 1 on the stack, because the `exit` statement only breaks out of it's scope.

## Further Reading