# Switch

**Switch statement** compares a variable with one or many given values and executes a block of code associated with this value when a match is found. Starting from v4.0, Sanny Builder supports it for all games.

### Syntax

<pre class="language-pascal"><code class="lang-pascal"><strong>switch &#x3C;var>
</strong>   case &#x3C;n1>, &#x3C;n2>, ... &#x3C;n3>
     // do something if &#x3C;var> is equal to n1, n2, or n3
   case &#x3C;n4>
     // do something if &#x3C;var> is equal to n4
   default
     // do something if none of the values above matched the variable
end
</code></pre>

`<var>` a global or local [variable](/language/data-types/variables.md). Can be an integer, float, or string variable.\
`<n>` - a comma-separated list of values the variable is compared with. Sanny uses `==` [operator](/language/instructions/expressions.md) to compare values. `n` can be an integer number, float, string, also a [constant](/language/data-types/constants.md).

{% hint style="info" %}
Each case can be associated with up to `8` values.
{% endhint %}

Default case is optional and can be omitted. When provided, default must be the last in the list of cases.

Switch statement does not support any extra instructions outside of `case` or `default` blocks. It also does not support a "fall through" behavior usually found in C-like languages. When the case block ends, the control flow is transferred out of the switch statement.

#### Example

<pre class="language-pascal"><code class="lang-pascal"><strong>switch 0@
</strong>   case 1, 2, 3
     0ace: "Value is 1, 2, or 3"
   case 5
     0ace: "Value is 5"
   default
     0ace: "Value is not 1, 2, 3, or 5"
end
</code></pre>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sannybuilder.com/language/control-flow/switch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
